0

I have a widget that I need to add a category select list to. The end user should be able to select a category and I need to save the category ID with the widget.

I've hit a stumbling block in that I cannot get my exclude array to work. The excluded cats still show up in the dropdown. What am I doing wrong?

function form( $instance ) {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'hide_title' => '', 'category_id' => '' ) );
    $title = format_to_edit($instance['title']);
    $text = format_to_edit($instance['text']);

    $hide_title = $instance['hide_title'] ? ' checked="checked"' : '';
    $category_id = $instance['category_id'] ? ' selected="selected"' : '';
    ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
        <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" class="widefat" />
    </p>
    <select id="<?php echo $this->get_field_id( 'category_id' ); ?>" name="<?php echo $this->get_field_name( 'category_id' ); ?>"> 
     <option value=""><?php echo esc_attr(__('Select a Category')); ?></option> 
     <?php 
        $args = array('exclude' => array(get_cats()),'hide_empty' => 0 );
        $categories=get_categories($args); 
        foreach ($categories as $category) {
            $option = '<option value="'.$category->cat_ID.'">';
            $option .= $category->cat_name;
            $option .= '</option>';
            echo $option;
        }
     ?>
    </select>

function get_cats(){
    $exclude_cats = array(
        get_cat_ID('test1'),
        get_cat_ID('test2'),
        get_cat_ID('test3'),
        );
    return $exclude_cats;
}

2 Answers 2

1

Per http://codex.wordpress.org/Class_Reference/WP_Query you would need to prefix the numerical identifier of the array with a minus-sign, such as $query = new WP_Query( 'cat=-12,-34,-56' );, which would exclude the categories with the identifier 12, 34 and 56.

You could achieve this by changing the get_cats function into the following:

function get_cats(){
    $exclude_cats = array(
        get_cat_ID('test1'),
        get_cat_ID('test2'),
        get_cat_ID('test3'),
    );
    foreach ($exclude_cats as $item) {
        $array[] = '-'.$item;
    }
    return $array;
}

Which returns the array exactly as before, except each item has a minus-sign prepended to it.

2
  • Thanks OleVik, but I'm not using WP_Query, I'm using get_categories(). Its supposed to be able to take an exclude argument like so get_categories('exclude=1,2,3')
    – Scott B
    Commented Jan 17, 2012 at 20:58
  • Could you try printing the get_cats()-function? The problem may be that $args = array('exclude' => array(get_cats()),'hide_empty' => 0 ); is not receiving a formatted array (ie; exclude=10,12,14), but just outputs the numbers in a bulk.
    – OleVik
    Commented Jan 18, 2012 at 18:06
0

get_cats() already returns an array, so using 'array' in here is too much:

'exclude' => array(get_cats()),

try:

'exclude' => get_cats(),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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