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'm lost, I don't understand why this works

http://website.com/?tag=my-cat-slug

and not this

http://website.com/?tag=15

It's really annoying because wp_dropdown_categories() only provides ids as values.

I tried to change permalinks to default settings instead of rewriting:

http://website.com/?p=123

But it changed nothing

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Because the tag query variable expects the value to the terms slug. It'll be looking for the term with slug '15' (which presumably doesn't exist).

And, yes its quite frustrating that wp_dropdown_categories() uses the ID as the value, rather than the slug. This is because it was originally used only for categories (for which IDs rather than slugs are generally used) as opposed to general taxonomies.

However, there is this ticket on it. I have created this gist, which can allow you to make wp_dropdown_categories() use slugs rather than IDs for the value.

For reference (the following class should go in a plug-in):

/* 
 * A walker class to use that extends wp_dropdown_categories and allows it to use the term's slug as a value rather than ID.
 *
 * See http://core.trac.wordpress.org/ticket/13258
 *
 * Usage, as normal:
 * wp_dropdown_categories($args);
 *
 * But specify the custom walker class, and (optionally) a 'id' or 'slug' for the 'value' parameter:
 * $args=array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... );
 * wp_dropdown_categories($args);
 * 
 * If the 'value' parameter is not set it will use term ID for categories, and the term's slug for other taxonomies in the value attribute of the term's <option>.
*/

class SH_Walker_TaxonomyDropdown extends Walker_CategoryDropdown{

    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ){
        $pad = str_repeat('&nbsp;', $depth * 3);
        $cat_name = apply_filters('list_cats', $category->name, $category);

        if( !isset($args['value']) ){
            $args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
        }

        $value = ($args['value']=='slug' ? $category->slug : $category->term_id );

        $output .= "\t<option class=\"level-$depth\" value=\"".$value."\"";
        if ( $value === (string) $args['selected'] ){   
            $output .= ' selected="selected"';
        }
        $output .= '>';
        $output .= $pad.$cat_name;
        if ( $args['show_count'] )
            $output .= '&nbsp;&nbsp;('. $category->count .')';

        $output .= "</option>\n";
        }

}

Usage

$args=array(
    'walker'=> new SH_Walker_TaxonomyDropdown(), 
    'value'=>'slug', 
     .... 
);
wp_dropdown_categories($args);

The 'value' parameter optional. The default value will be 'id' for categories, and 'slug' for other taxonomies.

share|improve this answer
    
Thanks Stephen, I had seen your solution on different websites. I ended up using it but all the options were selected="selected" on my homepage so I changed if ( $value == $args['selected'] ) into if ( !empty($args['selected']) AND $value == $args['selected'] ) I also had to replace if ( $args['show_count'] ) by if ( isset($args['show_count']) AND $args['show_count'] ) in order to get rid of all the notice errors and warning errors in PHP. –  Christian Jul 9 '12 at 7:34
    
Actually the only thing causing an error should be the 'show_last_update', which I shall remove. The rest should all be set because of they are sent through wp_parse_args (github.com/WordPress/WordPress/blob/3.4.1/wp-includes/…) –  Stephen Harris Jul 12 '12 at 8:58
    
I noticed it only because I submit with Javascript, otherwise I would not have guessed all the selected="selected". Concerning the *notice errors I notice them only because on my dev webserver I like to put WP_DEBUG=ON –  Christian Jul 12 '12 at 10:05
    
I have too :), but I don't get any errors regarding show_count or selected. They should exist in the $args because of the line linked to above. Concerning selected="selected" appearing everywhere... oops. Sloppy php coding. 'string' == 0 always returns true. I'll edit the answer... –  Stephen Harris Jul 12 '12 at 12:23
1  
@HarishChouhan the stric error is because the over-riding method doesn't declare the same number of variables as the method its over-riding method. (Fixed now). –  Stephen Harris Mar 15 at 14:48
show 3 more comments

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.