0

I'm building a taxonomy->child taxonomy dropdowns where the child will only be active if the parent is selected. Everything works great, except that I'm using wp_dropdown_categories for the parent and the value that I get is an ID of the selected item instead of slug or name.

For that reason I'm using a custom walker to get the name and it works great, my issue is that because I'm using this walker now, I cannot get the ID of the selected parent to activate the child dropdown.

  wp_dropdown_categories(array(
'show_count' => 1,
'selected' => 0,
'hierarchical' => 1,
'depth' => 1,
'hide_empty' => 0,
'tab_index' => $tabindex,
'class' => 'gt-select',
'show_option_none' => 'Select Product',
'name' => 'cat',
'id' => 'my-product',
'taxonomy' => 'products',
'walker' => new Walker_CategoryDropdown_Custom(),
));

The child taxonomy code is:

$cat_id = sanitize_text_field($_POST['cat']);
$cat_query = "child_of={$cat_id}&hide_empty=0&taxonomy=products";
$categories = get_categories($cat_query);
$options = '<option value="-1">Select Color</option>';
foreach ($categories as $cat) {
  $name = $cat->name;
  $value = $cat->term_id;
  $label = "{$cat->cat_name} ({$cat->category_count})";
  $options .= "<option value=\"{$name}\" data-value=\"{$value}\">{$label}</option>";
}

$return = array(
  'options' => $options,
);

The issue with using the Custom Walker is that now instead of getting the id here:

$cat_id = sanitize_text_field($_POST['cat']);

I'm getting the parent category name or slug, if I take the walker out everything works great but wp_dropdown_categories() will insert a new record with the ID number in the categories instead of updating the existing one.

I'm adding the Custom walker here just for reference:

class Walker_CategoryDropdown_Custom extends Walker_CategoryDropdown
    {
        function start_el(&$output , $category , $depth , $args)
        {
            $pad = str_repeat(' ' , $depth * 3);$cat_name = apply_filters('list_cats' , $category->name , $category);
            $output .= "\t<option class=\"level-$depth\" value=\"" . $category->name . "\" data-value=\"" . $category->term_id . "\"";
            if ( $category->term_id == $args[ 'selected' ] ) $output .= ' selected="selected"';
            $output .= '>';
            $output .= $pad . $cat_name;
            if ( $args[ 'show_count' ] ) $output .= '  (' . $category->count . ')';
            if ( array_key_exists('show_last_update', $args) && $args[ 'show_last_update' ] ) {
                $format = 'Y-m-d';
                $output .= '  ' . gmdate($format , $category->last_update_timestamp);
            }
            $output .= "</option>\n";

        }

    }

The value of the output is:

value=\"" . $category->name . "\" 

and the parent category works great updating itself in the database but the child category dropdown will not populate.

If I change this in the walker to:

value=\"" . $category->term_id . "\" 

The child will populate but the parent category will not update and it will insert itself as a new record with the ID number as the label.

Basically I need to find a way to get the selected category ID from wp_dropdown_categories() and pass that here:

$cat_id = sanitize_text_field($_POST['cat']);

1 Answer 1

1

May be easier to just work with it, than against it...

Instead of:

$cat_id = sanitize_text_field($_POST['cat']);

Try:

$cat_name = sanitize_text_field( $_POST['cat'] );
$cat = get_term_by( 'name', $cat_name, 'category' );
$cat_id = $cat->term_id;
2
  • Thank you for your help, I'll try this solution and I'll post my findings back.
    – r3b3l
    Oct 16, 2014 at 15:28
  • I need to do more tests but I believe it is working correctly with: $cat_name = sanitize_text_field( $_POST['cat'] ); $cat = get_term_by( 'name', $cat_name, 'products' ); $cat_id = $cat->term_id; $cat_query = "child_of{$cat_id}&hide_empty=0&taxonomy=products"; $categories = get_categories($cat_query);
    – r3b3l
    Oct 17, 2014 at 4:46

Your Answer

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

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