Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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() ) {
    $my_query->next_post();
    $post_ids[] = $my_query->post->ID;
}

wp_reset_postdata();

... and read on here.

share|improve this answer

Does something like this work?

query_posts('meta_key=XYZ&meta_value=ABC');
if (have_posts()) :
    while (have_posts()) : the_post();
        $post_id = get_the_ID() ;
        echo $post_id;
    endwhile;
else :
    echo 'Nothing';
endif;
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

 
discard

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.