Title: query posts question
Last modified: August 18, 2016

---

# query posts question

 *  Resolved [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/query-posts-question/)
 * I have (what I hope is) a quick question.
 * I’m doing a custom query so that the site will 1) look for a certain category,
   then 2) look for a certain meta key/value before posting the post. I can get 
   it so that if the post(s) in question fit the criteria of being in this one category,
   and has this custom field value set, it (or they) will display.
 * The problem is, I on;y one the most recent *one* to display, which I can get 
   to work about halfway. If I have a newer post showing, then the older post (that
   meets my criteria above) disappears, and nothing shows at all. I’m positive it
   has to do with the “showposts=1” I have in my new query – it seems to search 
   the most recent post made, and if it fits the criteria, great. If not, it won’t
   search further to see if an older posts meets it.
 * My query looks like so:
 *     ```
       <?php
       $newcat_query = new WP_Query('category_name=' . $newcatslug . '&showposts=1);
             while ($newcat_query->have_posts()) : $newcat_query->the_post();
   
             $key = "featured_post";
             $single = "true";
             $meta = get_post_meta($post->ID, $key, $single);
   
             if($meta == "true") {  ?>
       // display here
       ```
   
 * Like I said, if I remove the “showposts=1” part, it’ll do what I want it to do
   BUT if more than one post meets the criteria, then they’re all displayed. If 
   I leave it in , it’ll work *only* if the newest post meets the criteria.
 * Anyone know how I can fix this? I thought perhaps I could the the current `$post-
   >ID` and compare it to the next oldest one if it doesn’t meet the criteria, but
   I can’t seem to code that correctly to make it function. I also thought perhaps
   there was a way to just *stop* the Loop once it found the most recent post that
   fits the criteria, but again, I can’t seem to code that properly, either.
 * Any help would be appreciated 🙂

Viewing 1 replies (of 1 total)

 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/query-posts-question/#post-637768)
 * Got it! Woo-hoo!
 * Okay – in case anyone else needs it:
 *     ```
       <?php 
   
        // get Featured posts
             $newcat_query = new WP_Query('category_name=' . $newcatslug);
             $postcount=0;
             while ($newcat_query->have_posts()) : $newcat_query->the_post();
   
             $key = "featured_post";
             $single = "true";
             $meta = get_post_meta($post->ID, $key, $single);
   
             if($meta == "true") {
             $postcount++;
             if ($postcount == 1) :?>
   
       //post layout here
   
        <?php endif; }
        endwhile; ?>
       ```
   
 * It was the addition of the “$postcount” that did the trick – find the posts, 
   and after diksplaying the first one it finds, it’ll stop. Yay!

Viewing 1 replies (of 1 total)

The topic ‘query posts question’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * Last activity: [18 years, 7 months ago](https://wordpress.org/support/topic/query-posts-question/#post-637768)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
