filter out “uncategorized” from all category listings with one filter in functions.php?

I’d like to add code to my functions.php to intercept all calls to wp_list_categories() AND the_category() so that it excludes “uncategorized” and any children of “uncategorized” (OR cat_id 1). I’d also like to limit the number of words returned in the “title” attribute of each list item anchor. Currently the default is to set $cat_args[‘use_desc_for_title’] = 1;
but It does not appear it’s possible to insert that into the exclusions filter below.

I’m using the code below which effectively removes the unwanted categories from the lists that use wp_list_categories, however, it does not remove them from lists derived from the_category…

function my_list_terms_exclusions($exclusions,$args) {
  $children = implode(',',get_term_children(1,'category'));
  $children = (empty($children) ? '' : ",$children");
  return $exclusions . " AND (t.term_id NOT IN (1{$children}))";
}
add_filter('list_terms_exclusions', 'my_list_terms_exclusions', 10, 2);?>

Related posts

Leave a Reply

1 comment

  1. I was finally able to figure this one out… Although I have to set the filter directly on the widget_categories_args method, so it does take two filter statements to do it.

    function my_categories_filter($cat_args){
        $cat_args['title_li'] = '';
        $cat_args['exclude_tree'] = 1;
        $cat_args['exclude'] = 1;
        $cat_args['use_desc_for_title'] = 0;
        return $cat_args;
    }
    
    add_filter('widget_categories_args', 'my_categories_filter', 10, 2);