I need to find if a post with a custom field X equal to Y exists in a wordpress installation.

Should I do it with a simple sql query or is there something build in that can help me achieve it?

3 Answers
3

You can use the WP_Query(); to check that like this:

$my_query = new WP_Query();
$my_query->query(array( 'meta_key' => 'X', 'meta_value' => 'Y'));
if ( $my_query->have_posts() ){

     //it exists

} else {
    //it's not here
}

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *