• Resolved Doodlebee

    (@doodlebee)


    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

    (@doodlebee)

    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.