How do I get a meta value from WP_Query?

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
}

Leave a Comment