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

2 Answers 2


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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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