Last active
October 5, 2022 02:47
-
-
Save jonathanstegall/f7454efac3b0e8a78a3c963c18197ced to your computer and use it in GitHub Desktop.
get a count of stories for each category by year/month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT t.term_id, REPLACE(t.name, '&', '&') as name, COUNT(p.ID) as post_count, YEAR(post_date) as year, MONTH(post_date) as month | |
| FROM wp_terms t | |
| INNER JOIN wp_term_taxonomy tax ON t.term_id = tax.term_id | |
| INNER JOIN wp_term_relationships r ON tax.term_taxonomy_id = r.term_taxonomy_id | |
| INNER JOIN wp_posts p ON r.object_id = p.ID | |
| WHERE tax.taxonomy = 'category' and YEAR(post_date) >= YEAR(CURDATE()) and p.post_type = 'post' and p.post_status = 'publish' | |
| GROUP BY term_id, YEAR(post_date), MONTH(post_date) | |
| ORDER BY YEAR(post_date) DESC, MONTH(post_date) DESC, t.name ASC; |
Author
Author
If we just want the totals for the comment above, we can do this:
excluding stuff:
SELECT COUNT(p.ID) as post_count, YEAR(post_date) as year
FROM wp_terms t
INNER JOIN wp_term_taxonomy tax ON t.term_id = tax.term_id
INNER JOIN wp_postmeta m ON REPLACE( REPLACE( m.meta_value, 'a:1:{s:8:"category";s:5:"', '' ), '";}', '' ) = t.term_id
INNER JOIN wp_posts p ON m.post_id = p.ID
WHERE tax.taxonomy = 'category' and YEAR(post_date) >= YEAR(CURDATE()) and p.post_type = 'post' and p.post_status = 'publish' and m.meta_key = '_category_permalink'
AND t.term_ID NOT IN (55575, 55623, 55577, 55590, 55619, 55622, 55630, 55628)
GROUP BY YEAR(post_date)
ORDER BY YEAR(post_date) DESC;including opinion only:
SELECT COUNT(p.ID) as post_count, YEAR(post_date) as year
FROM wp_terms t
INNER JOIN wp_term_taxonomy tax ON t.term_id = tax.term_id
INNER JOIN wp_postmeta m ON REPLACE( REPLACE( m.meta_value, 'a:1:{s:8:"category";s:5:"', '' ), '";}', '' ) = t.term_id
INNER JOIN wp_posts p ON m.post_id = p.ID
WHERE tax.taxonomy = 'category' and YEAR(post_date) >= YEAR(CURDATE()) and p.post_type = 'post' and p.post_status = 'publish' and m.meta_key = '_category_permalink'
AND t.term_ID IN (55577, 55623)
GROUP BY YEAR(post_date)
ORDER BY YEAR(post_date) DESC;
Author
To do a better group by over several months (for a year, for example) do this:
SELECT t.term_id, REPLACE(t.name, '&', '&') as name, COUNT(p.ID) as post_count, EXTRACT(YEAR_MONTH FROM post_date) as yearmonth
FROM wp_terms t
INNER JOIN wp_term_taxonomy tax ON t.term_id = tax.term_id
INNER JOIN wp_postmeta m ON REPLACE( REPLACE( m.meta_value, 'a:1:{s:8:"category";s:5:"', '' ), '";}', '' ) = t.term_id
INNER JOIN wp_posts p ON m.post_id = p.ID
WHERE tax.taxonomy = 'category' and EXTRACT(YEAR_MONTH FROM post_date) >= 202110 and p.post_type = 'post' and p.post_status = 'publish' and m.meta_key = '_category_permalink'
AND t.term_ID NOT IN (55575, 55623, 55577, 55590, 55619, 55622, 55630, 55628)
GROUP BY term_id, EXTRACT(YEAR_MONTH FROM post_date) DESC
ORDER BY EXTRACT(YEAR_MONTH FROM post_date) DESC, t.name ASC;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If we want all stories published this year but include or exclude desired categories, we can do it like this:
Categories to exclude:
Categories to include for only opinion: