How to Show WordPress Posts from a Single Category Only

(Note: See comments on this post for other options. Also, see this post.)

If you’ve ever wanted to show only posts from a single WordPress category somewhere, such as on your homepage or in a template file that your create, then it’s pretty easy to do; however, it does take a little digging into your theme’s files.

Editing Your Theme’s Files

In order to display posts from one category, you’ll want to find “The Loop” in the file where you want those posts to display. In the examples below, we’re going to be putting our code into our homepage.

In many themes, you will easily be able to find The Loop for your homepage in your index.php file. (Appearance > Editor > Main Index Template – index.php)

The Loop is the code that begins like this:

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

Some themes these days put the loop in a separate file and call it into the index.php file with other code. An example of this can be found in the default WordPress TwentyTen theme.

If you have a case like that, you can just find the spot where the loop is being called in, and place your code above that.

Here’s a look at the code in the TwentyTen theme. It shows you where you can place your code.

Featured Plugin - WordPress Ecommerce Shopping Cart Plugin

Out of all the WordPress ecommerce plugins available, MarketPress has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Code for Showing Posts from Only One Category

There are a number of ways you could achieve this, but perhaps these two are the easiest: You can choose to pull the category posts in via the Category ID number or the Category Name.

We’ll start with using the category name.

Let’s say you have a category titled “Foreign Cars.” Here’s the code for that.

<?php query_posts('category_name=Foreign Cars'); ?>

And here’s what it looks like in my theme’s file. (Note: I’m not using the TwentyTen theme here, so you can see the actual beginning of The Loop.)

As mentioned, you can also achieve the same thing by using the Category ID Number. If you aren’t sure how to find the ID, see the following post: How to Find the IDs of WordPress Categories, Posts, Pages, and More.

When you have your ID number, then insert code like the following (replacing the number with the number of your category). In this example, my category ID is 14.

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

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Showing Posts from More than One Category

Either of the methods above will show the posts from only the category you choose, but what if you’d like to show posts from two categories (or three or four)?

That’s easy enough to do. You simply take one of the methods above and add a little more code to it. In this example, let’s say I want to show posts from my category titled “Foreign Cars” and my category titled “Fast Boats.”

Here’s how I’d do that:

<?php query_posts('category_name=Foreign Cars, Fast Boats'); ?>

And if I’m using category IDs instead of category names, here’s how I’d do it.

<?php query_posts('cat=14,15'); ?>

Control Number of Posts

OK, so the code above should get you the categories you’d like, but you can get a lot more control than that by adding a few other parameters.

One of the most common that someone might like is to control the number of posts you want to appear. So, for example, you might want your Foreign Cars category to show, but you only want three posts to show (not the default ten).

In order to achieve that, you’d have code like this (add &showposts=3):

<?php query_posts('category_name=Foreign Cars&showposts=3'); ?>

Change the number 3 to whatever number you like.

If you’re using Category IDs, then the code would look like this (again, just add &showposts=3):

<?php query_posts('cat=14&showposts=3'); ?>

And you can also do this even if you’re showing multiple categories as in the example above (add &showposts=3):

<?php query_posts('category_name=Foreign Cars, Fast Boats&showposts=3'); ?>

Using category IDs it would look like this:

<?php query_posts('cat=14,15&showposts=3'); ?>

Note: In the example above, this will NOT pull three posts from each category. It will only pull three posts total (the latest three posts).

Featured Plugin - WordPress Google Maps Plugin

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

Filter by Tag

Another popular parameter you may want to filter by is tags. Here are a few examples of that. We’ll only pull posts from the Foreign Cars category that are tagged “red cars” (note that we put a hyphen in for tags if it’s two or more words)

<?php query_posts('category_name=Foreign Cars&tag=red-cars'); ?>

And using category IDs it would look like this:

<?php query_posts('cat=14&tag=red-cars'); ?>

Below are some more popular parameters you may want to use.

Show Posts in Ascending Order

By default, WordPress shows post in descending order. In other words, it starts with the last post and puts it at the top (it descends down from a high numbered post to the lowered numbered posts). If you like to reverse that, you’ll need to tell WordPress to show posts in ascending order (starting with the lowest numbered post).

<?php query_posts('category_name=Foreign Cars&order=ASC'); ?>


<?php query_posts('cat=14&order=ASC'); ?>

Show Posts in Alphabetical Order (A – Z) by Title

<?php query_posts('category_name=Foreign Cars&orderby=title&order=ASC'); ?>


<?php query_posts('cat=14&orderby=title&order=ASC'); ?>

Show Posts in Reverse Alphabetical Order (Z – A) by Title

<?php query_posts('category_name=Foreign Cars&orderby=title'); ?>


<?php query_posts('cat=14&orderby=title'); ?>

Featured Plugin - WordPress Membership Site Plugin

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

Filter Posts by Author

This will pull only posts from this author (Bill) in the selected category (Foreign Cars).

<?php query_posts('category_name=Foreign Cars&author_name=Bill'); ?>

Or using the category ID and the author ID:

<?php query_posts('cat=14&author=3'); ?>

Stacking Parameters

As you can see, you can add lots of parameters to filter the posts. You can play around with them until you get what you want.

For example, let’s set up some code to do the following:

  • Pull only posts from the Foreign Cars category (category_name=Foreign Cars)
  • By the author named Bill (&author_name=Bill)
  • With a tag “red cars” (&tag=red-cars)
  • Let’s show only five of them (&showposts=5)
  • And lets order them so that the oldest one shows at the top (&order=ASC)

And here’s our code for that:

<?php query_posts('category_name=Foreign Cars&author_name=Bill&tag=red-cars&showposts=5&order=ASC'); ?>

Pull in Your WordPress Pages

Another little related trick will let you pull in a Page onto the front page. You would do that like this with the Page ID (in this example, the ID is 22):

<?php query_posts('page_id=22'); ?>

If you’d like to pull in multiple pages, then it’s a little more complicate. Let’s pull in Page 22 and Page 25.

$args = array(
'post_type' => 'page',
'post__in' => array(22,25)

Now that you have the hang of this, you can check out a page where you can find even a few more parameters.

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Photo: Export Icon from BigStock

Comments (12)

  1. Hello

    I think it’s better to hack the query instead of create a new one.
    Use “pre_get_posts” hook to hack it, this is best for performance, the request is already done when you crate yours, so 2 requests, just “why !?”

    See you

  2. Hi. Sorry, but this is wrong, looks like you have no clue of what query_posts does, and why you shouldn’t use it, to do what you’re trying to do. As @BoiteAWeb pointed out, you should be using the “pre_get_posts” action or the “request” filter, to alter the main query.

    If you want to know why, watch Andrew’s session about WP_Query from WordCamp Portland:

    Have a good day,

  3. I think I found that using category_name gave me unwanted posts if my category happened to be a child category. I’ve been using something like:

     * Do not display child, grandchild, etc. posts when viewing a parent category
     * and order by title in ascending order unless on the home screen
     * @param array $query
     * @return modified $query
     * @author Jon Breitenbucher
    function jb_no_child_posts( $query) {
        global $wp_query;
        $id = $wp_query->get_queried_object_id();
        if ( !is_home() ) {
            if ( $query->is_category ) {
                $query->set( 'category__in', array( $id ) );
                $query->set( 'orderby', 'title' );
                $query->set( 'order', 'asc' );
            return $query;
    add_action('pre_get_posts', 'jb_no_child_posts');

    I haven’t checked recently to see if I am still following best practices with this method.

  4. re: Boite’s comment and Konstantin’s slightly more snarky one – just make sure to work in a check for the query you are modifying in that function with the pre get hook, as it can modify more than what you bargain for.

    Here’s a sample from the codex:

    Here’s a sample from an event conference site I’m working on now which adds a custom post type to the site’s main feed:

    //Adds Conference Updates post type to home page blog loop

    function custom_conference_in_home_loop( $query ) {

    if ( is_home() && $query->is_main_query() )
    $query->set( ‘post_type’, array( ‘post’, ‘conference-updates’) );

    return $query;
    add_filter( ‘pre_get_posts’, ‘custom_conference_in_home_loop’ );

    As Andrew Nacin mentions in the video, he also recommends using ‘New WP Query’ through which you can pass arguments that match the conditional tags you need, like excluding categories, posts per page, or post types.

    Either way, pre get is pretty powerful to tap that already defined query.


  5. Joe, great post. I am a newbie to WordPress, recently migrated from Joomla. I am a now a convert. :)

    I love how you covered off on a lot about controlling posts on the front page.

    One question I have is related to having one, two or more categories on the front page and randomizing it too.

    I have tried some randomizing solutions I have found on the web, but they tend to double up the posts if they are in two categories.

    Any suggestions?

    • Tom – I’m afraid I’m not enough of a coder to know the answer to that. You might ask in the forums. The WordPress section of Stackexchange is also a good place to go for WP coding help.

  6. Hi Joe!

    Thanks for writing this great guide. It helped me a lot.

    I want to thank you by notifying you that you have wrong inverted commas inside the code people should copy.


    First inverted commas: ‘
    Second inverted commas: ’

    These second inverted commas make the code not working.

    Thanks again for this great guide,

  7. Useful and clear post, thanks.
    I have been struggling to find a way to pull posts from one or more categories into a Page.
    Using your method, I created a page template based on index.php, then inserted your code to pull posts from one category.
    Then I just created a Page to which I applied the template, and it’s the best way I have found to display posts from a category.
    The multiple advantages over other methods I found were:
    - the “read more” tags still work fine,
    - so do links to older posts.
    However, I wonder if there is any “hidden” disadvantage to this method.
    Any thought?