This is a follow-up post to my Excluding Categories from Different Parts of WordPress post a while back. The problem I was having is that the posts in my Testing and Daily Tweets categories where being counted and shown in the Archives drop-down widget. For example, it showed 4 posts in April 2011. When selecting April 2011 in the drop-down, you would get the “404 Not Found” page since all 4 posts belonged in the Daily Tweets category which is excluded from showing in the main blog, except when being accessed via the Category drop-down.
After some searching, I found the following thread: wp_get_archives and Conditional Tags and adapted the code as follows in my theme’s functions.php file:
add_filter( 'getarchives_join', 'customarchives_join' );
function customarchives_join( $x ) {
global $wpdb;
return $x . " 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( $x ) {
global $wpdb;
// 61 = Daily Tweets, 74 = Testing
$exclude = '61,74'; // category id to exclude
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
}
Now, it does not count those posts in the Archives drop-down. Yay! 🙂
Hello,
Thank you for your code it is very helpful however I’ve run into a unique use case where I need to exclude not only the parent category but any children categories with the excluded parent. Your code seems to work as expected when dealing with categories but doesn’t seem to work with child categories. Any idea how it might be edited to include the child categories?
Thanks.
Well, the simplest way would be to exclude each of the child category IDs as well in the list. Basically treating each one as a category. Can find the IDs in the wp_terms database. I might play around with it and see if I can come up with a better way.
Hello Mark,
I did try adding both the parent category and the child categories but it didn’t seem to work. Yet when I add a category that has no child categories the code works as expected. Any other thoughts?
Found a problem which is probably the cause. If the post is in more than one category and one of those categories is not excluded then it will still show in the Archives drop-down list. I’ll have to play around with it.
Hello Mark,
Thank you for your reply. That was exactly it. I had one post that in addition to being in the parent and the child categories, was also in another category and that tripped it up. Once I removed that post from the second category (leaving it in the child and parent category) and adding both parent and child category ID’s to your code everything worked correctly. Thank You!
Going back to the parent child relationship, I tried to change the code to include the parent and all child cat’s it contained without having to list each child out but I couldn’t get it to work. This would be useful so that if more child categories were added in the future by an end user, it would automatically be hidden and the functions.php file wouldn’t have to be edited further. For now I’ve got everything working but if wanted to expand the functionality this suggestion might be useful. Thanks again.