Skip to content

Instantly share code, notes, and snippets.

@rudwolf
Created July 14, 2016 17:57
Show Gist options
  • Save rudwolf/201565592dddd276e287b51d883bc60f to your computer and use it in GitHub Desktop.
Save rudwolf/201565592dddd276e287b51d883bc60f to your computer and use it in GitHub Desktop.
Get the archive for specific category
<?php
global $wpdb, $wp_locale;
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts, $wpdb->term_taxonomy, $wpdb->term_relationships
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->term_taxonomy.term_id = 11
AND $wpdb->posts.ID = $wpdb->term_relationships.object_id
AND $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
$arcresults = $wpdb->get_results($query);
foreach ($arcresults as $arcresult):
$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);?>
<li><a href="<?php bloginfo('url') ?>/[your_category_base]/[your_category_name]/date/<?php echo $arcresult->year; ?>/<?php echo str_pad($arcresult->month, 2, '0', STR_PAD_LEFT); ?>"><?php echo $text; ?> </li>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment