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'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
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.