Run Loop on Posts of Specific Category

<?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. User Avatar
    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?

    • User Avatar
      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…

    • User Avatar
      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. User Avatar
    Permalink to comment#

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

  3. User Avatar

    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?

    • User Avatar
      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. User Avatar
    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. User Avatar
    Permalink to comment#

    where I have to placephp wp_reset_query();?

    • User Avatar
      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. User Avatar
    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. User Avatar
    Permalink to comment#

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

  8. User Avatar
    Permalink to comment#

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

  9. User Avatar
    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. User Avatar
    Permalink to comment#

    thanks a lot for sharing this with us thanks

  11. User Avatar
    Permalink to comment#

    Nice. Very well explained.

  12. User Avatar
    Permalink to comment#

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

    • User Avatar
      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. User Avatar
    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. User Avatar
    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. User Avatar
    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. User Avatar
    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. User Avatar

    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. User Avatar
    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. User Avatar
    Permalink to comment#

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

  20. User Avatar
    Permalink to comment#

    It looses the sticky functionality, how to keep it?

  21. User Avatar
    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. User Avatar

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

  23. User Avatar
    Permalink to comment#

    Great! Thanks a lot!

  24. User Avatar
    Permalink to comment#

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

  25. User Avatar
    Permalink to comment#

    Thanx, great help!
    my question is that i am not sure where to place the code to limit eg the categories (so to say pick posts from 3-4 categories).
    Which php files from the theme am using shoud be altered?
    I have on the right side bar a recent posts block and a slider on my first main page which displays posts.
    thanks in advance!!

  26. User Avatar
    Permalink to comment#

    Nice ! You saved my website :’) <3<3

  27. User Avatar
    Permalink to comment#
    im unable to show some post on front page this code doesn’t work for me ? i’m showing featured category on front page

  28. User Avatar
    Permalink to comment#

    Thanks for posting this article.

    I have a situation like i need to display 3 posts of category “sports” in a div .And In a another div i want to display the next three posts from the same “sports” category.Please give me suggestions regarding this.
    thanks in advance.

  29. User Avatar
    Permalink to comment#

    Totally new to WP and came across this recently, partly because my pagination for posts in a category is not yet working..

    You are using query_posts and afterwards resetting the query. By reading through above SE question and detailed explanation would you say it is still OK to use query_posts in this particular case?

  30. User Avatar
    Dave Hightower
    Permalink to comment#

    Thanks all!

  31. User Avatar
    Permalink to comment#

    How can i ask the div container to display none if there are no posts in the specific category?

    Thank you

  32. User Avatar
    Permalink to comment#


    How can I fetch Specific posts from a category to which a user subscribed. Any ideas on the PHP Code? (Users subscribe to categories using

    Thanks so much!!

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.