I need to find out the post_id of a post_meta record where the custom key is 'XYZ' and the is 'ABC'? Is there an API for that?

Or do I need to run a regular select SQL with wpdb class?


You should use WP_Query in this case ...

$my_query = new WP_Query( array( 'meta_key' => 'XYZ', 'meta_value' => 'ABC' ) );

$post_ids = array();
while ( $my_query->have_posts() ) {
    $post_ids[] = $my_query->post->ID;


... and read on here.


Does something like this work?

if (have_posts()) :
    while (have_posts()) : the_post();
        $post_id = get_the_ID() ;
        echo $post_id;
else :
    echo 'Nothing';
  • Thank you but, I was more interested in a direct API. I ended up running a straight forward SQL select and get to the post ID thus; SELECT post_id FROM wp_postmeta where meta_key='XYZ' and meta_value`='ABC' – Average Joe Jun 5 '12 at 1:25

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.