WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer

Does something like this work?

if (have_posts()) :
    while (have_posts()) : the_post();
        $post_id = get_the_ID() ;
        echo $post_id;
else :
    echo 'Nothing';
share|improve this answer
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 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.