简体   繁体   English

WordPress | WP_Query与meta_query问题

[英]WordPress | WP_Query issue with meta_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. 我为我的帖子创建了一个元框,可以保存三个值0、1、2之一。该值是通过选择控件选择的。

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. 实际的问题是,返回类别22中的帖子,但返回未将meta值设置为1的最新帖子。

any idea please ? 有什么想法吗?

Here are the data in my postmeta table in SQL Insert statemts: 这是我在SQL插入statemts中的postmeta表中的数据:

/*
-- 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');

maybe try this? 也许试试这个?

$args               =   

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

A Post Type declaration is required. 需要一个Post Type声明。 ( Learn something new everyday. post_type defaults to post ). (每天学习新知识。post_type默认为post)。 Something doesn't seem right in your statement then. 那时您的陈述中似乎有些不对劲。 Are you sure the meta_key is 'postStatus'. 您确定meta_key是'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
        )
    )
) );

You meta_query should look like this : 您meta_query应该看起来像这样:

   'meta_query' => array(
       array(
           'key' => 'postStatus',
           'value' => 1,
           'compare' => '=',
       )
   )

Here's a link to the WP_Query class reference 这是WP_Query类参考的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM