Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Created December 8, 2014 06:32
Show Gist options
  • Save joshuadavidnelson/681d08913a36eda69121 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/681d08913a36eda69121 to your computer and use it in GitHub Desktop.
SQL to get years of posts in specific category
<?php
// Custom query. Change the number in parenthesis on line 10 to your category id
$query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts
FROM $wpdb->posts wp_posts INNER JOIN $wpdb->term_relationships
ON (wp_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)
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN (1)
$where GROUP BY YEAR(post_date)
ORDER BY post_date $order";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment