Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
1  
This is one of those situations where hacking your way trough filters to change the behavior of some widget is stupid. Just create your own widget - you'll write less, more readable and faster code. –  onetrickpony Sep 17 '11 at 23:12
add comment

1 Answer

up vote 0 down vote accepted

I have an 'actor' cpt and it seems to automatically get added to the query:

SELECT object_id, term_taxonomy_id
FROM wp_term_relationships INNER JOIN wp_posts ON object_id = ID 
WHERE term_taxonomy_id IN (38,40,30,11,32,34,29,39,35,9,31,19,33,37,42,41,27,25,36,1,26) 
AND post_type IN ('post', 'actor')
AND post_status = 'publish'

Call from: require, require_once, include, get_sidebar, get_template_part, locate_template, load_template, require, dynamic_sidebar, call_user_func_array, WP_Widget->display_callback, WP_Widget_Categories->widget, wp_list_categories, get_categories, get_terms, _pad_term_counts

(Using the Debug Queries plugin)

And this is the CPT definition:

register_post_type('actor', array(
  'public' => true,
  'labels' => $labels,
  'has_archive' => 'actors',
  'taxonomies' => array( 'category' )
));
share|improve this answer
    
Turns out you are right, it works but only on 3.3-aortic-dissection, not on 3.2.1 yet. :) –  GreenDude Sep 17 '11 at 23:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.