Group posts by Category
I want to use WordPress for a online magazine. I want to sort posts by the Category.
I’ve found this code snipet http://rhymedcode.net/1001-wordpression-loops/grouped-by-category/
but in this example the category number is always the same. But i want to show the most recent post ordered by Category.
Does anybody know how to do it?
Thanks and sorry for my bad English 🙂
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(); ?> - DO YOUR STUFF : TITLE, PERMALINK, AUTHOR, DATE, EXCERPT, ETC. - <?php endwhile; ?> </div> --- SECOND LOOP --- <div class="section"> <h1 class="section_title">SPORTS</h1> <?php query_posts('cat=2&showposts=5'); ?> <?php while (have_posts()) : the_post(); ?> - DO YOUR STUFF : TITLE, PERMALINK, AUTHOR, DATE, EXCERPT, ETC. - <?php endwhile; ?> </div> --- THIRD LOOP --- <div class="section"> <h1 class="section_title">POLITIC</h1> <?php query_posts('cat=3&showposts=5'); ?> <?php while (have_posts()) : the_post(); ?> - DO YOUR STUFF : TITLE, PERMALINK, AUTHOR, DATE, EXCERPT, ETC. - <?php endwhile; ?> </div>
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…) …
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: http://uk.reuters.com/
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
Post from 17.12.2008
Post from 16.12.2008
Post frim 15.12.2008
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(); $which_category=$category->cat_ID; if (!($which_category==$is_category_shown)) : <div class="home-cat"><p class="home-categorie"> <?php the_category(' '); ?></p> </div> <?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->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 :
<?php 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";')); query_posts(''); ?>
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 🙂
- The topic ‘Group posts by Category’ is closed to new replies.