Custom order of posts on main page
The most common home page on WP powered site contains posts listed in chronological order (from the newest to the oldest one). But sometimes we need different layout. Fortunately WP is quite flexible and allows to create many customized lists of posts.
I’m not going to list all of them since I guess such list would never end up. But I will show some of the most popular customizations including required changes.
All changes will be applied to index.php file (the one within folder with theme we use) since that’s the one responsible for main page look.
I have already written about two custom main pages:
- displaying one post plus list of titles of other posts – Main Page – one post plus list of titles of other posts
- excluding one particular category from main page – http://www.nietoperzka.com/wptraining/about-category-xphp-file/ (first paragraph)
But they use different functions.
Here I would like to show how functions like <?php $posts=query_posts($query_string . ' '); ?>
, <?php query_posts(" "); ?>
and <?php $temp_query = $wp_query; query_posts(' '); ?>
can be used.
The best one (in my opinion ;) ) is the first one. It has one important advantage over the other two. If we set to display 10 posts per page(in admin panel -> option -> reading) and we have more then 10 posts meeting the condition we set in our index.php file functions <?php query_posts(" "); ?>
and <?php $temp_query = $wp_query; query_posts(' '); ?>
WON’T work properly. On following pages we will see the same posts as on the main page.. That’s why I would recommend them only in situations when number of posts we want to display on main page is smaller then number of posts per page set in admin panel.
The most common usage (in my practice):
1. <?php $posts=query_posts($query_string . ' '); ?>
In index.php file we need to replace <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
with below piece of code to have:
- posts listed in reverse chronological order (from the oldest to the newest one)
<?php $posts=query_posts($query_string . '&order=asc');
if (have_posts()) : while (have_posts()) : the_post(); ?> - posts ordered alphabetically
<?php $posts=query_posts($query_string . '&orderby=title&order=asc');
if (have_posts()) : while (have_posts()) : the_post(); ?> - posts from categories *extras* and *images* only
<?php $posts=query_posts($query_string . '&category_name=extras,images');
if (have_posts()) : while (have_posts()) : the_post(); ?> - posts from category *extras* published in *October*
<?php $posts=query_posts($query_string . '&category_name=extras&monthnum=10'); if (have_posts()) : while (have_posts()) : the_post(); ?>
- posts by user *Neo*
<?php $posts=query_posts($query_string . '&author_name=neo');
if (have_posts()) : while (have_posts()) : the_post(); ?>
2. <?php query_posts(" "); ?>
In index.php file we need to replace <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
with below piece of code to have:
- posts published on particular day of the month (for example if we always publish reports on the 25th of the month )
<?php query_posts("day=25"); if (have_posts()) : while (have_posts()) : the_post(); ?>
- posts tagged with tag *scripts*
<?php query_posts("tag=scripts"); if (have_posts()) : while (have_posts()) : the_post(); ?>
- posts containing one of listed tags
<?php query_posts("tag=bread+baking+recipe"); if (have_posts()) : while (have_posts()) : the_post(); ?>
- posts from category *extras* published in June (for example)
<?php query_posts("category_name=extras&monthnum=6"); if (have_posts()) : while (have_posts()) : the_post(); ?>
Of course we can mix parameters to achieve required result. We can set to display for example posts written by Neo which belong to category *extras* an so on..
3. function <?php $temp_query = $wp_query; query_posts(' '); ?>
need to be inserted BEFORE <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
(in idex.php file) and requires inserting <?php $wp_query = $temp_query; ?>
right after <?php endif; ?>
. That reason (in addition to limited usability I’ve mentioned at the beginning) makes it not really useful ;). Hence I won’t show you any example since function <?php query_posts(" "); ?>
will be just enough.
NOTE! Very often it is said that instead of category_name=extras we can use cat=5 (where 5 is an ID number of category extras) but.. many times I had problems to make it working hence I prefer to use parameter category_name.
29th December 2007 18 comments
Love the post. I was not aware that you could do that much with this.
I’m looking for the php code to show 1 random article on a page and can’t find it. Do you know how you can do this ?
Thanks
Hi Neil,
There is quite a lot plugins for displaying random posts plus some codes you can insert manually to the file.
.
The point is they usually dspaly only title of post (linked to post itself) but no text. Is it what you are looking for?
If yes, check on for example this one
found this site when trying to configure the wordpress homepage to show more post.
and its done by changing the query post to show 20 blog post. here is the code:
Hi powweb,
Please use < code > and < /code > tags to dispaly code :). Otherwise it disappears.
Thanks! This has been very helpful.
[…] 2.custom order of posts on main page […]
Very helpful. Thanks.
You’re welcome
. Great to hear you’ve found my post helpful.
Hi, I have a metadata “readed book” (libros leidos), how can i use query_posts to retrieves all posts only with this metadata? in sql “where readed_book like “Tom Sawyer”…
Sorry for my english
thanks.
hello,
i’m trying to make an index page that displays thefeed in two different divs that are side by side. the one on the left has the most recent post, and i am trying to get the one on the right to show the second most recent post. do you know what command i would use to achieve this?
any thoughts are much appreciated!
@ Damian
Sorry for late reply but things are changing and getting very busy here ;).
There is a great article on WP Codex:
» http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
that should help you to find working solution. Unfortunately I didn’t have time to check it on my own but it looks
promising. Good luck!
@ Billy
I’m sorry but I don’t see connection between the post I wrote above and your question. Maybe you should try to ask your question on official WordPress Forum.
[…] 2.custom order of posts on main page […]
[…] nietoperzka’s Custom order of posts on the main page […]
[…] if (have_posts()) : while (have_posts()) : the_post(); ?> ] I found the above code at Custom order of posts on main page WordPress Experiments But in light of the use of hooks, I’m not sure where to put it. (I did download your hooks plugin, […]
[…] http://www.nietoperzka.com/wptraining/custom-order-of-posts-on-main-page/ Hoort bij: WordPress — admin […]
[…] nietoperzka’s Custom order of posts on the main page […]
[…] メインページに記事のnietoperzka のカスタムオーダー […]