WordPress archive for single category

During a new WordPress project I’d the problem to generate an archive just for the “News” category and only for posts until 1998. The wordpress function wp_get_archives dosn’t support such parameters and I also wanted to use the Archive Widget in the sidebar.

At first I added some filters in functions.php:

// join term_relationships and term_taxonomy
 add_filter( 'getarchives_join', 'customarchives_join' );
 // add where condition for category
 add_filter( 'getarchives_where', 'customarchives_where' );
 // add where condition for date
 add_filter('getarchives_where','filter_until_year_archives');
function customarchives_join($join_clause) {
 global $wpdb;
 return $join_clause." 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($where_clause) {
 global $wpdb;
 $include = get_cat_ID('news');  // category id to include
 return $where_clause." AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
 }
function filter_until_year_archives($where_clause) {
 $year = '1998.01.01 00:00:00';
 return $where_clause." AND post_date > '".$year."'";
 }

Now you can use the Archive Widget and you’ll only get an archive for the “News” category with posts older than 1998-01-01.

Last of all it’s necessary to edit the archive template in your theme. Because you’ll still get posts from all categories on your archive page.

Add this code to archive.php after get_header and before the loop:

query_posts( $query_string . '&cat='.get_cat_ID('news') );

It’s working on this site: politik-digital.de/category/news/. I applied the Flexo Archive Widget for the archive list in the sidebar .