WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"' : '';
        <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" />
    <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> 
        $args = array('exclude' => array(get_cats()),'hide_empty' => 0 );
        foreach ($categories as $category) {
            $option = '<option value="'.$category->cat_ID.'">';
            $option .= $category->cat_name;
            $option .= '</option>';
            echo $option;

function get_cats(){
    $exclude_cats = array(
    return $exclude_cats;
share|improve this question
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(
    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

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

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


'exclude' => get_cats(),
share|improve this answer

Your Answer


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.