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

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 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败