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 24 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 [...]
Hello,
To use WordPress as a CMS, I would like to achieve one of these two things:
1) in category pages, to display posts ordered by post_name (not by post_title); post_name (i.e. post’s slug) on my site is not the sanitized version of the post’s title and is set to be: refXXX, where XXX is a product reference number, identical to the custom field value of custom field key Reference — see below)
or
2) in category pages, to display posts ordered by the value of a custom field; custom field key is Reference, custom field value is a number (obviously, a product reference number)
The presentation of category pages, except for the order of posts, should remain the same as the one already defined for category archives (i.e. it’s not a simple list of posts that is needed).
The pagination should work correctly.
Is it possible with WordPress at the moment? My WordPress install is 2.5.1.
I believe I’ve now read all the Wordpress Codex and support pages related to “post order”, “order by slug”, “order by post name”, “order by custom value”, “custom post order” and so on…
I’m certain I’ve bothered for way too long helpful and dedicated developers — special friendly thanks to Camu from http://www.duechiacchiere.it/ …
Below is the code that Camu eventually came with. The code works, but imperfectly (problem where posts belong to several categories; problem with the pagination):
Catégorieterm_id;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->term_relationships wjoin, $wpdb->term_taxonomy wcat
WHERE wposts.ID = wjoin.object_id
AND wcat.term_taxonomy_id = wjoin.term_taxonomy_id
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
AND wcat.term_id = $currentCatID
ORDER BY wposts.post_name ASC
LIMIT 0,10
";
$posts = $wpdb->get_results($querystr, OBJECT);
}
// If I have posts to display
if (have_posts()):
// Now I have ordered posts, I can display them (the Loop)
foreach ($posts as $post_count => $post){
start_wp();
$myReference = get_post_meta($post->ID, "Référence", true);
// display article informations and content
?>
< img src="//-01.jpg" width="100%" >
Référence : · ID, 'post_tag', ", ' - ', " ) ; ?>
HELP!
Feel free to get in touch with me about this issue if you think of a solution. I’m beginning to feel desperate
Well, the php code got all mangled above…
If you’re interested in solving the problem, e-mail me and I’ll join the code in my reply.
[...] nietoperzka’s Custom order of posts on the main page [...]
Hi Neil,
Many thanks it helps me a lot. More success
Blessings
When I did this I was no longer able to use a static page for the home page.
Maybe you can help me. I am using this code with mixed results. I have a couple sites I am running WP2.6 on and this works on some and not on others. Here’s what I am doing…
First off, I want to be able to dynamically create a new page that pulls content from posts. For example, if I want to create a page about dogs, I want it to pull all posts published in the dogs category. I am using the Custom Fields of the page to add category as the key and the category name (dogs) in the value, to know where to pull the information. To determine this when the page loads, I have used the following code:
ID,’category’,$single=true); ?>
I have then added the following code (using includes to keep it clean) to show the posts for “dogs” on the page:
Related Articles:
<a href=”" rel=”bookmark”>
Written by
Funny thing (frustrating things actually) is that it works PERFECTLY on one website, but will not on another and I can’t find ANY differences. I am using the same exact code. I know it is pulling the category because I added to the else part and it prints the correct category. So it is just not pulling the content in the loop.
I did try your method of :
But that showed only the PAGE named after the category (same page I am on!)
I am lost why the loop works on one site but not the other. Any ideas? This is killing me!
Ed
Holy cow… it just ate all my code!
Let me try this…
First off, I want to be able to dynamically create a new page that pulls content from posts. For example, if I want to create a page about dogs, I want it to pull all posts published in the dogs category. I am using the Custom Fields of the page to add category as the key and the category name (dogs) in the value, to know where to pull the information. To determine this when the page loads, I have used the following code within the PHP call:
$cat=get_post_meta($post->ID,’category’,$single=true);
This pulls the category from the Custom Fields and stores it as $cat
Next, I use the following code:
$post=query_posts(”category_name=$cat&posts_per_page=5″); if (have_posts()) : while (have_posts()) : the_post();
Then the normal display stuff for the loop.
Funny thing (frustrating things actually) is that it works PERFECTLY on one website, but will not on another and I can’t find ANY differences. I am using the same exact code. I know it is pulling the category because I added to the else part and it prints the correct category. So it is just not pulling the content in the loop.
I did try your method of :
$posts=query_posts($query_string . “&category_name=$cat”);
if (have_posts()) : while (have_posts()) : the_post();
But that showed only the PAGE named after the category (same page I am on!)
I am lost why the loop works on one site but not the other. Any ideas? This is killing me!
Ed
hello!
can i change the order of posts by the ID of comments?
[...] 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, [...]
i want to query posts published in one month ago ,and display in random ,then how would i do?
[...] http://www.nietoperzka.com/wptraining/custom-order-of-posts-on-main-page/ Hoort bij: Wordpress — admin [...]