How to display the latest post from each category using WordPress query_posts
By default WordPress presents all the posts in the blog sorted descending to post time at the home page. Well in some circumstances you might not want to have that layout on your home page. Lets say you have five categories and you want to pull the latest post from each of the categories and display it on your home page. Now that wont work out with the default WordPress home page layout. Here is a simple tip to work around that situation. WordPress has a function to filter out the posts depending on certain criteria, the function is known as query_post. The template function query_post has several parameters that enables you to filter the posts for the WordPress post output Loop. Below is an example of pulling the latest post from a category and display it at the blog home page. The query_post function can be modified by many parameters. Here I’m displaying only few parameters in action.
Example: Display the latest post from a category at homepage or display each latest post from multiple categories. This file is the index.php of your theme file.
'Sites',
'showposts'=>1,
'order'=>DESC)
); ?>
No Archive Found
Sorry, but you are looking for an archive that isn't here.
'Downloads',
'showposts'=>1,
'order'=>DESC)
); ?>
No Archive Found
Sorry, but you are looking for an archive that isn't here.
Example: Exclude some categories from the loop
Example: Retrieve a single specific post
Example: Get a sticky post
get_option('sticky_posts')); //returns array of all sticky posts
// To return just the first sticky post in a query
$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0]);
?>
These are just simple examples, you can do a lot more if you take a look at the WordPress Codex for query_posts.
Some of the link on this post may have affiliate links attached. Read the FTC Disclaimer.
You’ve made it this far! Please stay connected by receiving updates via email.
