WordPress: Limit Archives to Single Category

David Nash mysql, php, wordpress 9 Comments

Late yesterday afternoon a client asked if I could look at a business’s WordPress installation. They had posts in several categories but only wanted to show the “Latest News” posts in the archives.

WordPress is designed around blog posts, whereas I find that many business or company sites are designed around pages (eg About Us, Contact Us) – and don’t use WordPress’ powerful blogging tools on the front page. My site is an example – the content on the home page doesn’t change that much.

This means that many of the solutions are also geared around blog-post design.

While searching I found a few examples of how to exclude a specific category from the archives, when what I needed was to include only one category in the WordPress archives – generated in the theme by wp_get_archives().

The solution is to add a filter that modifies the SQL used by wp_get_achives() to the theme’s function.php:

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}

function customarchives_where( $x ) {
global $wpdb;
$include = '1'; // category id to include
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}

Replace ‘1’ with the category ID (wp-admin > posts > categories > edit, then in the address bar it is directly after ‘&tag_ID’) and that’s it. wp_get_archives() will now only display posts from that category.

I found this in http://wordpress.org/support/topic/wp_get_archives-and-conditional-tags?replies=6#post-1578041 (thanks Paul!).

Comments 9

  1. How do we ge this to show only 1 category? Right now, it also displays “UNCATEGORIZED”… how do we removed it and only display the category using 1 single category id… i only want to show one category.

    1. Post
      Author

      If you only have one category (by default WordPress calls it “Uncategorized”) you shouldn’t need to do anything. If you want to hide the word “Uncategorized” you would need to edit the theme file where this is displayed – eg sidebar.php

  2. Hi there, any idea how to edit this so that it shows only the archives of whatever category i’m in? I want to feature different categories on dedicated pages, and wish to customize the archives to only show the category of that page. Is this possible?

    1. Post
      Author

      Hi Emanuel

      That’s a little tricky because it’s possible that the currently viewed post is in multiple categories.

      If each post is just in one category, or if you’re happy just using the first category, you can replace the line:

      $include = ‘1’; // category id to include

      with

      $cat = get_the_category();
      $include = $cat[0]->cat_ID; //include current category

      and I think that should work – I haven’t tested it though.

    1. Post
      Author

      Thanks Shaina!

      I would probably create a category, one each for “blog1” and “blog2”. And then use .htaccess URL rewriting so that mysite.com/blog1 pulls content from mysite.com/cat/blog1 (I’m sure there’s a wordpress plugin that will provide URL rewriting for you too).

Leave a Reply

Your email address will not be published. Required fields are marked *