How to display the latest post from each category using WordPress query_posts

Advertisement

Wordpress Tips Tricks Tutorials

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 . The template function query_post has several parameters that enables you to filter the posts for the WordPress . Below is an example of pulling the latest post from a category and display it at the blog home page. The 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)
); ?>

Posted Under -

Written by on .

Get Free Updates - Weekly

Sign up for the free tips on business and productivity today.
Receive email only when we have new content.


Read More |

No Archive Found

Sorry, but you are looking for an archive that isn't here.



'Downloads',
'showposts'=>1,
'order'=>DESC)
); ?>

Posted Under -

Written by on .


Read More |

Advertisement

Divi - Huge Savings

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 .

Some of the link on this post may have affiliate links attached. Read the FTC Disclaimer.

20 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments