Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got the custom post type foo with meta fields called bar and baz. Now I get the WP_Query object using:

$args = array( 
       'post_type'      => 'foo', 
       'meta_key'       => 'bar', 
       'meta_value'     => $bar_value,
       'posts_per_page' => 10,
);
$res = new WP_Query($args);

Now I want to check if the value baz of the selected post is equal to $baz_value. How can I do that?

share|improve this question
1  
See meta_query for WP_Query –  Milo Oct 10 '13 at 14:18

3 Answers 3

The query arguments are in several places in the query object. You can var_dump the object and see them.

  1. $res->query_vars
  2. $res->meta_query (in this case)
  3. $res->query

Items 1 and 3 will be easiest to work with. Simple PHP object and array syntax will get the information you need.

share|improve this answer
up vote 0 down vote accepted

I've found a solution to my problem myself. Since none of the already existing answers solved the problem, I figured I could best post mine here to help people with similar problems.

The solution:

if($res->have_posts()) { 
    $id = $mail_res->posts[0]->ID; // blindly assuming there is only 1 post having baz = baz_value
    $true_baz = get_post_meta($id, 'baz')[0];

    if($true_baz== $baz) {
        //success
    } else {
        //error
    }
} else {
    //error
}
share|improve this answer

Did you tried this one:

$args = array( 
   'post_type'      => 'foo', 
   'meta_key'       => 'bar', 
   'meta_value'     => $bar_value,
   'posts_per_page' => 10,
);
$res = new WP_Query($args);

 to 
  $args = array( 
   'post_type'      => 'foo', 
   'meta_key'       => 'bar', 
   'meta_value'     => $bar_value,
   'meta_compare'     => '=',
   'posts_per_page' => 10,
);
$res = new WP_Query($args);

Read this: WP_Query() — WordPress Codex

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.