Get a free trial // Grow your CSS skills // Land your dream job

Run Loop on Posts of Specific Category

Last updated on:
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <?php the_content(); ?>
<?php endwhile; endif; ?>

If you were to use this, for example, in a left sidebar which ran before the main loop on your page, remember to reset the query or it will upset that main loop.

<?php wp_reset_query(); ?>


  1. Jake
    Permalink to comment#

    Is there a way to display a certain number of items and then have a link at the bottom for older posts?

    • Webweaver
      Permalink to comment#

      Yes to question1. I have imported the most recent posts of a category into my website by using query_posts('category_name=whatevernameyouuse &posts_per_page=6');

      I have not looked at linking just to older posts, I just linked to my blog but I bet there is a way to do it…

    • Webweaver
      Permalink to comment#

      Or for a more authoritative look at how to do recent posts, look on this website for the recent posts article.

  2. ciptard
    Permalink to comment#

    veru nice..
    thanks for sharing..
    i need this tuts for my web project..

  3. Nico

    I want to create a loop that contains only posts that are in two specified categories – I just don’t get how to do this. Is anyone here to help me out of this problem?

    • ubrayj02
      Permalink to comment#

      See where it says cat=5?

      <?php query_posts('cat=5'); ?>

      Make it say cat=5,6,7

      Then, you will have fetched posts from categories 5, 6, and 7.

      If you want to limit the number of posts, stick a “showposts” in there to make something like this:

      <?php query_posts("showposts=2&cat=13,14"); ?>

  4. Brian
    Permalink to comment#

    This is awesome. Where are the valid places I can use this snippet of code? I want to display a custom loop on a page.

  5. Ferik
    Permalink to comment#

    where I have to placephp wp_reset_query();?

    • Pierre
      Permalink to comment#

      I personally place it after the ?php endwhile; endif; ?> I don’t know if it is a good practice but seen to work fine for me!

  6. Ben Yates
    Permalink to comment#

    Your post was a great help.

    The only difficulty I am having is setting the category to show from an id entered into a custom field when writing the post.

    I’ve tried PHP echo and grabbing the field using a plugin but still struggling to set the category based on what the custom field value is.

    Any help would be greatly appreciated.


  7. hamish
    Permalink to comment#

    Hey Chris,
    How would you add an if empty statement to this? Many thanks

  8. Vinicius
    Permalink to comment#

    Thanks Mate! It really saved my life!!!!!!!

  9. Mandip
    Permalink to comment#

    can i get the code to display the rest post of the same category in single page on which the viewer is in?

  10. fiamm
    Permalink to comment#

    thanks a lot for sharing this with us thanks

  11. amakups
    Permalink to comment#

    Nice. Very well explained.

  12. DSQ
    Permalink to comment#

    Hi there, can anyone please tell me how to query custom posts form a certain category? I’ve tried everything. Thanks!

    • Ryan
      Permalink to comment#

      I’m also looking for the same answer as DSQ, when I’m running query_posts – this quires all posts in the db instead of the category you are on… Presuming because it’s outside the loop, or because there is no loop.

      Is there any way you can query the loop? So for example I have a category page and I only want to display posts with the tag ‘example’ in that category, is this possible?

      I’ve been at it for hours! Also just found this which has given me some new ideas

  13. Elise
    Permalink to comment#

    Is there a way to do this so that it puts all the post from one category under that category and then loops to do the same for the next category. I Basically want it to do some think like this: Category 1 – Post 1 – Post 2 – Post 3 Category 2 – Post 1 – Post 2 – Post 3 Category 3 – Post 1 – Post 2 – Post 3 etc.

  14. Linda
    Permalink to comment#

    I used the almost the same code as advised on this post, minor tweaks
    <?php query_posts('showposts=2&category_name=slug'); ?>
    so, showposts sets how many posts are displayed
    category_name=slug works great now that category ID has been deprecated.

  15. David Wickman
    Permalink to comment#

    Totally used this today, but modified it a bit as I needed to include all posts except the ones that are in the home page slider category. To do this, just add a minus sign in front of the integer like so:

    <?php query_posts('cat=-5'); ?>

    Thanks for the post!

  16. Brian
    Permalink to comment#

    Is there a way to have a post, rotate at midnight or daily from a specific category and not repeat for a year?

  17. Dario

    Hi there! I have a problem i hope someone can help me because i’m freaking out! I need to display the latest 4 post in my home page, but the problem is that i can’t use a simple loop because they are located in different part of my home page. So if I simply use a loop it will display me the latest 4 post in the space of 1. I kind of need to treat like an array so i can say” In this position show the last post” and ” in this other position show the one before the last” etc. I hope i explained myself pretty clear! Thanks to everyone.

  18. Roland
    Permalink to comment#

    This snippet was very usefull for me.
    Thanks. It works fine.

    How is it possible to show posts from different categories on different pages?
    I want to display posts from category7 on page1, posts from category3 on page2 and so on.
    I’m struggling with the php-code for the if-loop. Can anyone explain a little bit?

    Thank you.

  19. kalai
    Permalink to comment#

    Here we can use wp_get_recent_posts() function directly for get posts

  20. Alberto
    Permalink to comment#

    It looses the sticky functionality, how to keep it?

  21. Web_Mistress00
    Permalink to comment#

    Here’s a question.. I have categories that I want to display on specific dates. Is there a way to run the loop to display posts of a certain category depending on the date? Like if I have a category named Jan-12, and I want to modify the loop to look at the current date and find/display the category with the same month/day (no year), is this possible?

  22. john

    I want to make my mobile press theme display posts for different categories at the index page how can i do this?

  23. Santosh
    Permalink to comment#

    Great! Thanks a lot!

  24. Goran
    Permalink to comment#

    This is pretty bad practice now, query_posts should be skipped and get_posts used instead.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed