Search code examples
phpwordpresstaxonomy

WordPress Widget Categories hook


I see there is a get_the_categories hook. This doesn't seem to effect the categories output by the default WP categories widget.

function wp_cat_filter($categories) {
  var_dump($categories); // I'd like to remove a category before it's output.
} 
add_filter('get_the_categories','wpr_cat_filter');

This works great and I have an I have an object to work with here but not when it comes to the widget? I'm looking to remove a category here.

Is there a hook for the widget categories specifically and wouldn't that call the get_categories function?


Solution

  • I think you should be able to use the widget_categories_args filter:

    This filter is used by the default WordPress Widget: Categories before it passes arguments to the wp_list_categories() function.

    For example, something like this might work:

    function wpr_cat_filter($args) {
      // remove category 1, 2 and 3
      $exclude = array(1, 2, 3);
    
      if (isset($args['exclude'] ) && !empty($args['exclude'])) {
         $exclude = array_unique(array_merge(explode(',', $args['exclude']), $exclude));
       }
       $args['exclude'] = implode(',', $exclude);
       return $args;
    }  
    
    add_filter('widget_categories_args', 'wpr_cat_filter');