Displaying Archives by month, excluding a category
-
Hi All,
I have a category of a site I’m working on that should be distinct from the main blog area. I needed to display an archive list that excluded this category.
This is what I came up with,
<ul> <li><h2>Archives</h2></li> <?php $querystr = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code> , count(ID) as posts FROM $wpdb->posts 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) WHERE $wpdb->term_taxonomy.term_id != 12 AND $wpdb->term_taxonomy.parent != 12 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC"; $years = $wpdb->get_results($querystr); foreach ( (array) $years as $year ) { $url = get_month_link( $year->year, $year->month ); $date =mysql2date('F o', $year->year.'-'.$year->month, $translate = true); echo get_archives_link($url, $date, 'html','<li>',' (' . $year->posts .')</li>'); } ?> </ul>
Let me know if I’ve just wasted and hour and theres and easier solution!
Change $wpdb->term_taxonomy.term_id != 12 to the ID of the category to exclude, and also $wpdb->term_taxonomy.parent != 12 to the ID of the category for it to apply to subcategories.
Hope someone finds it useful.
{P.S The wp get archives function should have an exclude parameter!)
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Displaying Archives by month, excluding a category’ is closed to new replies.