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

link|improve this question

feedback

2 Answers

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

UPDATE

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

link|improve this answer
feedback

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(
    array(
        '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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.