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'm having custom field "prime" with values yes or no. I want to get the post id's with selected value "Yes".How can i get that.

Thanks in advance

share|improve this question
up vote 2 down vote accepted

Try something like this

$posts = get_posts( array(
    'numberposts' => -1,
    'meta_key' => 'prime', 
    'meta_value' => 'yes' 
    ) );

$post_ids = array();

if ( $posts ) {
    foreach ( $posts as $post ) {
        // Push post's IDs into array
        array_push( $post_ids, $post->ID );

code isn't tested but it should work. If you don't wont post ids into array just replace whole array_push line with $post->ID


Set 'numberposts' argument to -1, so it will return all posts not only 5 as default. Thanks to @Brady

share|improve this answer

Personally I would use a custom SQL query to do this as then I'm returning just the ID's that I need. But to do it the WP way you can use this:

$posts = get_posts(
        'numberposts'     => -1,
        'meta_key'        => "prime",
        'meta_value'      => "yes",

$posts will hold an array of objects. These objects hold all the post data you would need for a loop etc

share|improve this answer

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.