Skip to content

Instantly share code, notes, and snippets.

@de-raaf-media
Created November 23, 2012 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save de-raaf-media/4135538 to your computer and use it in GitHub Desktop.
Save de-raaf-media/4135538 to your computer and use it in GitHub Desktop.
wordpress function hook - wp_get_archives by category
// 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."'";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment