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

Please wait

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.

Get 5GB Free Space on the Cloud for your Devices

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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

1
2
3
4
5
<?php
   if (is_home()) {
      query_posts("cat=-1,-2,-3");
   }
?>

 

Example: Retrieve a single specific post

1
2
3
4
<?php
  // retrieve one post with an ID of 5
  query_posts('p=5');      
?>

 

Example: Get a sticky post

1
2
3
4
5
6
7
8
<?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 .

About Ruhani Rabin

Ruhani Rabin is the original owner and author of this blog. He also reviews web 2.0 startups at Tech2all.com. Largely interested in web 2.0 apps & Social Media. Currently the Vice President of MOL Access Portal (MOL is owner of Friendster.com). Also he is Web 2.0 & Social Media Researcher and a Total fun Geek!.. There you have it ;)

Facebook Comments

  • http://www.applenmicro.com/ jailbreak iphone

    Awesome work Ruhani

  • http://www.rebateshut.com/ get cashback

    thanks for the code

  • http://gobookmarks.info/user.php?login=scottjames71&view=history Hui Bernier

    Big Thanks for the article. Added to bookmarks permanently .Amazing looking website really!

  • http://www.mydietarystore.info/ Fred Genti

    The steps given are so easy to understand that I made it worked perfectly! Thanks a lot.

  • http://www.mydietarytown.info/ Wanda amers

    I’ve been searching for this. Thanks a lot for sharing. It will surely help in finding the comments easily.

  • http://www.delicious.com/ritabowersox Lorraine Walker

    I wish you the best with your new blog!

  • Dex

    Thanks so much for this – I have looked far and wide for some code that actually works and you didn’t dispoint.

  • http://www.k9stud.com Puppies For Sale

    thanks for your tutorial. it can help to filter post by category

  • http://www.pridecarpetcleaning.com/ Rug Cleaning Plantation

    great script. i’ll try it .thanks for the post .

  • http://www.moldremoval.net Mold removal contractor

    thanks for your tutorial.i will try it .Great find.

  • Mark Vernik

    Great find. However, I am trying to make a custom function in thesis. I have the code working properly. But I would like to add a thumb each post. This has been eluding me for some time. Any suggestions?

  • http://www.moldremoval.com/f.florida-mold-removal-contractor.jacksonville.32207.html Mold Removal Jacksonville

    Great script. Thanks. Awesome.

  • http://bani.deocazie.info Bani Pe Net

    great script. i ll try it

  • Gahigf

    http://www.aceally-rack.com/Gondola-c9.html
    thank you for share,good post

  • http://www.webaddlink.com/ addlink addurl

    Your website is beautiful and useful. How do I access your site often. Come visit my website at addlink addurl and Linking to websites

  • http://gisnap.com gisnap

    Its really cool, I came to know this really worth visiting, just bookmarked your site.

    http://gisnap.com/
    The place where fun never ends

  • itjobs1

    thanks

  • http://blog.taufikarief.com/ taufik

    awesome. thanks for your tutorial. it can help to filter post by category

  • http://www.ruhanirabin.com Ruhani Rabin

    This code should replace the code at your header area and outside the loop

  • meda

    Do you place these code in the loop or out of the loop?

    I tried using this but it said No Archive Found.

  • Pingback: How to display a custom list of posts for wordpress blog which | Wood TV Stand

  • Pingback: Wordpress, loop multipli per una home page. Esempi di codice @ Suzupearl.com