The code below if from my WP plugin which gives a filtered listing of categories excluding the "uncategorized" category from display. However, when the user chooses "Show Hierarchy" from the widget setup options, the resulting display includes "uncategorized".

Given that I've placed 'exclude_tree' => 1 into the $cat_args array. What am I missing?

class My_Widget_Categories extends WP_Widget {

    function My_Widget_Categories() {
        $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "filters out uncategorized categories" ) );
        $this->WP_Widget('my_categories', __('my Categories'), $widget_ops);

    function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title']);
        $c = $instance['count'] ? '1' : '0';
        $h = $instance['hierarchical'] ? '1' : '0';
        $d = $instance['dropdown'] ? '1' : '0';

        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;
            $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude_tree' => 1);
        if ( $d ) {
            $cat_args['show_option_none'] = __('Select Category');
            wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
  • The code does not look complete, so it's hard to spot the error. – hakre Nov 23 '10 at 15:04

It's hard to say from your code. The exclude_tree parameter of wp_dropdown_categories() is pretty undocumented in codex. It's making use of get_categories() which does not list it at all.

If you don't have children within that category, you can consider to use the exclude parameter instead / additional. Just give it a test if that works. This might save you the hassle to deal with some bugs in wordpress in the end. Wordpress is not very well in handling hierarchy over the last years.

Some background info: Beta 2.8.2 - wp_list_categories buggy hierarchy - Ticket #9999; With wp_list_categories child categories not excluded when parents are excluded - Ticket #8614

  • thanks, there will definitely be child nodes off "uncategorized". It seems that the exclude_tree works on everything except the hierarchical display. With that said, adding $cat_args['exclude'] = 1 does the trick! – Scott B Nov 23 '10 at 15:14
  • uncategorized definitely has children and I was able to get exclude to work nonetheless. At least this is how it works on 3.01 – Scott B Nov 23 '10 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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