• anniebme

    (@anniebme)


    I have searched “filter latest posts by category” and don’t seem to be finding what I’m looking for.
    Here’s what I want to do:
    1. Grab the latest posts regardless of category
    2. Show the most recent post published.
    3. Show the next most recent post that has a different category
    4. Repeat third item one more time. (3 posts with different categories)
    5.I want the loop to be category agnostic: The author can make as many different categories as they want and as long as they meet the requirement of being a)the most recent post or b) the most recent post but a different category, it will show as one of the three posts displayed.

    Using stuffannforgottotellyou.com posts as an example,
    “Template files for wordpress themes” would show first, “A great fall day” would show second, “Makani” would be the third post in this monster I’m trying to create.

    Here is my loop so far:

    <?php query_posts('orderby=category&posts_per_page=3'); ?><!-- grab posts, order by category and only show three posts-->
    <!-- starting loop-->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
     <div class="post">
    
     <!-- Display the Title as a link to the Post's permalink. -->
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    <!--show off category-->
      <p class="postmetadata"><?php the_category(', '); ?></p>
    </div> 
    
    <!--ending loop-->
     <?php endwhile; else: ?>
     <p>Sorry, no posts matched your criteria.</p>
     <?php endif; ?>

    Basically, it’s showing Title and category. It’s listing posts, most recent first (so far so good) but not showing 1 per category. Would anyone like to take my n00b hand and help me out? What dumb thing have I forgotten?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Well, it is not as easy as you might think.

    First, there is no ‘orderby=category’. Even if there were, there is nothing to prevent the top 3 posts from being in the same category.

    Second, there is nothing preventing a post from belonging to more than one category, and so it could be listed more than once.

    Here is a link to a post that has a couple of answers.

    Thread Starter anniebme

    (@anniebme)

    Blogsessive.com has a tutorial that looks like its in the right direction that I’m looking. If anyone has a better way to do what I am trying to do… I’d love to see it ?? I’ll put my finished loop up for people to browse when I get it right.

    Thread Starter anniebme

    (@anniebme)

    Thanks, vtxyzzy! I’ll check that link and see what I can do. I appreciate your response–I had this page open for a long time and didn’t see your answer until after I submitted my blurb. I’m special.

    Thread Starter anniebme

    (@anniebme)

    Hi! I found code that works well enough for what I want!

    WordPress forum post that I *FINALLY* found! I knew I couldn’t be the only person who would want to try this.

    Here’s my edited version:

    <?php
                $catQuery = $wpdb->get_results(" SELECT * FROM $wpdb->terms AS wterms
    	INNER JOIN $wpdb->term_taxonomy AS wtaxonomy
    	ON ( wterms.term_id = wtaxonomy.term_id )
    	WHERE wtaxonomy.taxonomy = 'category'
    	AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0
    	ORDER BY wtaxonomy.term_taxonomy_id DESC
    	LIMIT 3");
    
                $catCounter = 0;
                foreach ($catQuery as $category) { //latest posts 
    
    				$catCounter++;
    				$catStyle = '';
    
    				if (is_int($catCounter / 3)) $catStyle = ' class="catAlt"';
    
    				echo $category->name;
    
    				query_posts('cat='.$category->term_id.'&showposts=1&orderby=date&order=desc'); //this controls the order of the posts with in categories ?>
    
    				<?php while (have_posts()) : the_post(); ?>
    
    					<?php the_post_thumbnail();?>
    					<?php the_title(); ?>
    
    				<?php endwhile; ?>
    
                <?php }  ?>

    Now I’m just adding bits and pieces of html to make it pretty. I hope this helps other curious people and many thanks to to everyone who helped–knowingly or not! ??

    Thread Starter anniebme

    (@anniebme)

    Ah, just kidding. This takes the latest categories made and displays their latest posts. When I tweak it more to show off latest three posts of different categories, I’ll post my code.

    Thread Starter anniebme

    (@anniebme)

    https://www.dagondesign.com

    I am currently trying to modify this plugin to do what I need it to.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filtering latest posts by category’ is closed to new replies.