3

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?

2

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
}
1

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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