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.

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;
}
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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 Jan 17 '12 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 Jan 18 '12 at 18:06
add comment

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

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

try:

'exclude' => get_cats(),
share|improve this answer
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.