So I got a fairly simple setup, one CPT with the default category
and post_tag
as taxonomies.
Say I have 1 post and 1 CPT entry assigned to the same category. I need this to be reflected inside the category widget as well.
I’ve managed to display both of them on the category page via the request
filter so the links generated by the widget work.
The problem is that if the post didn’t exist, the widget would not generate a link for the category (considering it empty).
Also, the item count is incorrect, 1 instead of 2.
Digging through the widget code, this is what’s causing the problems:
wp_list_categories(apply_filters('widget_categories_args', $cat_args));
So I can alter the arguments via the filter but wp_list_categories()
doesn’t have an argument for what post type it should use.
I’ve seen someone else encounter the problem before:
http://wordpress.org/support/topic/custom-post-types-and-category-widget – no luck
http://themehybrid.com/support/topic/custom-post-types-and-category-widget – same guy, can’t read Justin’s responses but I assume no luck again. he filed a ticket at the end which got closed
Thoughts on the solution
I’d really like to not copy/paste the default widget code and create a new one but solve this using a filter somehow.
That being said, would a custom walker help with this? Any ideas?
As a sidenote, I solved a similar issue with the Archives widget using this:
/*
* Add CPTs to wp_get_archives()
* http://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives
*/
function ucc_getarchives_where_filter( $where , $r ) {
$args = array( 'public' => true , '_builtin' => false );
$output = 'names'; $operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) ); $post_types = "'" . implode( "' , '" , $post_types ) . "'";
return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );
}
add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 );
Cheers!
I have an ‘actor’ cpt and it seems to automatically get added to the query:
(Using the Debug Queries plugin)
And this is the CPT definition: