KodeFor.Me
KodeFor.Me

Reputation: 13511

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.

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

Upvotes: 0

Views: 745

Answers (3)

Leon Rowland
Leon Rowland

Reputation: 61

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
        )
    )
) );

Upvotes: 2

John
John

Reputation: 21

maybe try this?

$args               =   

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

Upvotes: 2

Philippe Boissonneault
Philippe Boissonneault

Reputation: 3949

You meta_query should look like this :

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

Here's a link to the WP_Query class reference

Upvotes: 1

Related Questions