Display one post per category (3 posts)

  1. mrdthomas
    Posted 6 years ago #

    I am trying to figure out a way to display 3 of the latest post from 8 categories. I need only show 1 post from each category so if cat1 has 3 post that are newer than the other categories I still only want to display one of them.

    Breakdown: Page that displays 3 of the latest post from 8 categories. Doesn't matter which category post is not displayed due to it being limited to displaying 3, just cant show more that 1 per category.

    I cant figure out logically how to do this. Any ideas?


  2. esmi
    Forum Moderator
    Posted 6 years ago #

  3. MichaelH
    Posted 6 years ago #

    Austin Matzko put this on the wp-hackers list some time back...
    Latest post from each of six categories
    I would do something like this, where 1-6 are the category ids:

    $post_ids = array(0);
    foreach( array(1, 2, 3, 4, 5, 6) as $cat_id ) {
            if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 5)) ) {
                    $first = array_shift($posts);
                    $post_ids[] = $first->ID;
    query_posts(array('post__in' => $post_ids));

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.