WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the twenty eleven theme in my wordpress site.

In posts, I have two categories. One is blogs and other is News.

The monthly archive displays the posts of all categories while I want that the monthly archive should only display the posts of blogs category.

How will I do this?

I have also installed the wp_pagenavi plugin for the pagination.

share|improve this question
up vote 2 down vote accepted

There are two ways of doing it:

You can use a filter to alter the query when viewing an archive page. You will need to find the ID of your category 'blogs' (you can obtain it from the slug using get_term_by). Alternatively you can exclude a particular category by ID.

add_action( 'pre_get_posts', 'my_change_query'); 
    function my_change_query($query){
           $blog_term = get_term_by('slug', 'blogs', 'category');
           $blog_term_id = $blog_term->term_id;
           $query->set('cat', $blog_term_id);//Include category with ID $blog_term_id
           //$query->set('cat','-'.$blog_term_id);//Exclude category with ID  $blog_term_id
     return $query

or, more commonly, you can alter the archive.php template file and insert the following just above the if(have_posts()).

global $wp_query;
$args = array_merge( $wp_query->query, array( 'category_name' => 'blogs' ) );
query_posts( $args );

See the Codex on query_posts and WP_Query.

share|improve this answer
I used the the 2nd solution and it worked. Thanks – Ahmad Feb 1 '12 at 11:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.