
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.
<?php get_header(); ?>
<div id="content">
<!-- Latest from Category Sites -->
<?php query_posts(
array('category_name'=>'Sites',
'showposts'=>1,
'order'=>DESC)
); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<span class="categories">Posted Under - <?php the_category(', '); ?></span>
<p>Written by <?php the_author() ?> on <?php the_time('F j, Y'); ?>.</p>
</div>
<div class="entry">
<?php the_content(''); ?>
<span class="readmore"><a href="<?php the_permalink() ?>#more-<?php echo $post->ID; ?>">Read More</a></span> |<span class="addcomment"><?php comments_popup_link('Add Comments', '1 Comment', '% Comments'); ?></span>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="post">
<div class="title">
<h2>No Archive Found</h2>
</div>
<div class="entry">
<p>Sorry, but you are looking for an archive that isn't here.</p>
</div>
</div>
<?php endif; ?>
<!-- End of Sites Category Display -->
<!-- Latest from Downloads -->
<?php query_posts(
array('category_name'=>'Downloads',
'showposts'=>1,
'order'=>DESC)
); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<span class="categories">Posted Under - <?php the_category(', '); ?></span>
<p>Written by <?php the_author() ?> on <?php the_time('F j, Y'); ?>.</p>
</div>
<div class="entry">
<?php the_content(''); ?>
<span class="readmore"><a href="<?php the_permalink() ?>#more-<?php echo $post->ID; ?>">Read More</a></span> |<span class="addcomment"><?php comments_popup_link('Add Comments', '1 Comment', '% Comments'); ?></span>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="post">
<div class="title">
<h2>No Archive Found</h2>
</div>
<div class="entry">
<p>Sorry, but you are looking for an archive that isn't here.</p>
</div>
</div>
<?php endif; ?>
<!-- End of Downloads Category Display -->
<!-- Rest of the codes-->
Example: Exclude some categories from the loop
<?php
if (is_home()) {
query_posts("cat=-1,-2,-3");
}
?>
Example: Retrieve a single specific post
<?php
// retrieve one post with an ID of 5
query_posts('p=5');
?>
Example: Get a sticky post
<?php
array('post__in'=>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.