Support » Plugins » Group posts by Category

Viewing 9 replies - 1 through 9 (of 9 total)
  • Nobody knows?

    Hello NightWalker

    Fist advice : read this :

    When you’ll understand how wordpress “thinks”, it will be very easy for you to ask him what you want to do. WordPress thinks in “loop” to display all the posts from your blog. Basically, when you surf on a WordPress site, there is a main “default” loop to display all the posts, regardless of category.

    If I understand well, you want your main page to display on the index of your site different section for each category, just like in a newspaper site.

    To do this, you simply have to set a loop for each category.

    Let’s put it in a example with three category with their respective ID:

    Cinema : ID 1
    Sports : ID 2
    Politic : ID 3

    Then, as I said, wou will need to set three loop with a specific “query posts” for each of them :

    --- FIRST LOOP ---
    <div class="section">
    <h1 class="section_title">CINEMA</h1>
    <?php query_posts('cat=1&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    --- SECOND LOOP ---
    <div class="section">
    <h1 class="section_title">SPORTS</h1>
    <?php query_posts('cat=2&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    --- THIRD LOOP ---
    <div class="section">
    <h1 class="section_title">POLITIC</h1>
    <?php query_posts('cat=3&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>

    I wrote some “div” just to be clear. Of course, you will adjust this for your own needs with your own css.

    As you can see, each loop there have a specific “query post”. Let’s see, for instance, what happens with the first one :

    <?php query_posts('cat=1&showposts=5'); ?>

    In english, this code says to wordpress :

    Go get some post (query_posts) but only in category with the ID 1 wich is “Cinema” (cat=1) and display only the last five posts (showposts=5).

    That’s it. Of course, you can ask many other things to wordpress with query_posts, but here you have the main idea. You will find more by reading the links I gave you above. You can do this for as many catagory as you like.

    One of my main job is to manage the online version of a newspaper in Montreal. If you want to see it in action, just follow the “occupation” link in my profile (in french…) …

    Good luck.


    P.S. Sorry for the double empty post above… 🙂

    Thanks for your Answer!
    I’ve read a lot about the Templatetags and the loop.

    I want something like:
    but i don’t want to show the posts in ALL Category. It should only show the last 20 Posts ordered by Category.

    For example I’ve 5 Categorys Cat1,Cat2,Cat3,Cat4,Cat5 and at the main page the last 7 Posts are shown.


    Post from 18.12.2008
    Post from 15.12.2008

    **** Cat6

    Post from 17.12.2008
    Post from 16.12.2008
    Post frim 15.12.2008

    **** Cat2
    Post from 15.12.2008
    Post from 14.12.2008

    The last posts in Cat1 and Cat4 are from 11.12.2008 so this both cats doesnt shown at the Mainpage.

    I’ve try it with this code:

    <?php query_posts("orderby=category&order=DESC"); ?>
    <?php while ( have_posts() ) : the_post() ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <?php //To check if the category is alreasy shows to show the Category Name and Logo
    $category = get_the_category();
    if (!($which_category==$is_category_shown)) :
    <div class="home-cat"><p class="home-categorie">
    <?php the_category(' '); ?></p>
    <?php else : echo("<br />"); ?>					?>
    <?php endif; ?>

    At the end of loop

    <?php // TO set a number that the category is already displayed							$is_category_shown=$category[0]->cat_ID;

    My problem is, that WP doesnt sort the posts by Category with
    <?php query_posts("orderby=category&order=DESC"); ?>

    i don’t know why 🙁

    Oh you’ve done your second post some seconds before i’ve done my post.

    My problem is that there are also Categorys like Christmas, Eastern, Worldcup so that there are few posts in a limited period like only december for Christmas. Those Category should not shown all the time, only if there are new posts in these Categorys.

    I hope you know what i mean 🙂

    NightWalker wrote : “My problem is, that WP doesnt sort the posts by Category with
    <?php query_posts(“orderby=category&order=DESC”); ?>”

    Well… You are right. Although this kind of “orderby” is written in the codex, it doesn’t seem to work. I made some test on my test installation and you are right.

    At first, I was guessing that the codex did’nt take in consideration that categories in wordpress are no longer categories… :-)… In fact, they are now “terms”, along with “tags”, and the distinction is made via the term_taxonomy table…

    I don’t know if I’m right, but this sould be the reason whys “orderby=category” doesn’t work anymore…

    Then, after some testing, I found this thread where filosofo give a precious advice :

    So, replace your “<?php query_posts(“orderby=category&order=DESC”); ?>” with this piece of code :

    add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";'));
    add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'category\'";'));
    add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id DESC";'));

    And it does the trick… :-). Now, the post are sorted by category…

    You can even put this code in your functions.php to make a function that you will use before your loop when you want to order by category.



    Thanks you! You’ve helped me a lot.
    But it seems to have some problems. Sometimes new posts doesn’t shown at the mainpage. I’m now reading the Taxonomy at the codex i hope i can solve it by my self 🙂

    Thank you very much
    Looking for the same…


    SimonJ, filosofo – you guys are stars. Was looking for this. It really should be reimplemented, as it’s an enormousely useful feature – for example, making sense of search results. Shame it was retired in core.

    Thanks again for sharing your solution, guys!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Group posts by Category’ is closed to new replies.