douzhi9921 2013-02-13 18:28
浏览 57
已采纳

WordPress | meta_query的WP_Query问题

I have create a meta box for my posts, that can save one of the three values 0, 1, 2. The value is selected with an select control.

Also I have check my datebase records, and the meta data are stored in database, but I cannot query the meta values.

The code I use to query my posts containing meta values is the following:

$args               =   array(
    'category__in'      =>  array(
        22
    ),
    'posts_per_page'    =>  1,
    'meta_query'        =>  array(
        array(
            'key'          =>  'postStatus',
            'value'        =>  1,
            'compare'      =>  '='
        )
    )
);

$extremely_important    =   new WP_Query($args);

while($extremely_important->have_posts())
{
    $extremely_important->the_post();

    the_title();
}

The excact issue, is that return a post from category 22, but return the latest posts, that is not have the meta value set to 1.

any idea please ?

Here are the data in my postmeta table in SQL Insert statemts:

/*
-- Query: SELECT * FROM wp_postmeta WHERE post_id = 187754
LIMIT 0, 1000

-- Date: 2013-02-14 09:50
*/
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (16847,187754,'_edit_last','1');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (16848,187754,'_TIP_protect_images_post','unchecked');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (16849,187754,'_TIP_protect_text_post','unchecked');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (16850,187754,'_thumbnail_id','166897');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (16851,187754,'_fpp_is_scheduled','');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (18502,187754,'_edit_lock','1360791749:1');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (18503,187754,'post_status','extremely_important');
INSERT INTO `wp_postmeta` (`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES (18504,187754,'postStatus','1');
  • 写回答

3条回答 默认 最新

  • 普通网友 2013-02-14 07:42
    关注

    A Post Type declaration is required.( Learn something new everyday. post_type defaults to post ). Something doesn't seem right in your statement then. Are you sure the meta_key is 'postStatus'. Not in an array for example?

    $post_query = new WP_Query( array(
        'post_type' => '',
        'posts_per_page' => 1,
        'category__in' => array( 22 ),
        'meta_query' => array(
            array(
                'key' => 'postStatus',
                'value' => 1
            )
        )
    ) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题