Skip to main content
deleted 1775 characters in body
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38

So, I had to take the set_tax_query_field_to_id actionmake a custom walker and amenduse that with the tax_querywp_dropdown_categories() after it had been created.

Here is the full Class that I am using to add filters for my custom taxonomies to my various custom post typesfunction, as below -

class FGW_CPT_Taxonomy_Filter{
    
    /**
     * The customCustom postextended typeFGW_Cat_Slug_Walker toClass
 alter* andOutputs the taxonomies to show filters for
term 'slug' as the value *
of each option, not 'term_id' *(as @varis arraydefault)
     */
    privateclass $cptFGW_Cat_Slug_Walker =extends array();Walker_CategoryDropdown{
    
    /**
     * ConstructorStart the element output
     *
     * @param required array $cpt  string The$output custom post typePassed andby associatedreference. taxonomiesUsed to show filters for (as "array($cpt =>append array(tax1,additional tax2))")content
     */
  @param required publicobject function$category __construct($cptCategory =data array()){object
    
  * @param required int    $this->cpt$depth = $cpt;
  Depth of category in reference to add_action('restrict_manage_posts',parents. array($this,Default 'render_filter'));0
      * @param add_action('parse_tax_query',required array($this, 'set_tax_query_field_to_id'));
 $args     An array of arguments
    }
 * @param required 
 int    /**
$id     * Render ID of the taxonomycurrent filtercategory
     */
    public function render_filterstart_el(){
    
        global $typenow;
        
   &$output, $category, $depth = 0, $types$args = array_keysarray($this->cpt);    // Grab the post types that are being edited
        
        if(!in_array($typenow, $types))     // Check to see if the page now is one of the custom post types supplied by the user
           $id return= false;0){
        
        $filters$pad = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
        
        foreachstr_repeat($filters as $tax_slug) :
        
       ' ', $depth * 3);  $tax_obj = get_taxonomy($tax_slug); // GrabCreate the taxonomy objectpadding (from thebefore taxonomynested slugterms)
            
            $dropdown_args = array(             //** OutputGenerate the filter dropdown
                'show_option_all'   =>  $tax_obj->labels->all_items,
                'taxonomy'          =>  $tax_slug,
                'name'              =>  $tax_slug,
                'orderby'         HTML for =>this option 'name',*/
                'selected'          =>  (isset($_GET[$tax_slug]) ?$output.= $_GET[strtolowersprintf($tax_slug)] : null),"\t".
                'hierarchical'     '<option =>class="%1$s" value="%2$s" true%3$s>%4$s%5$s</option>',
                'depth'   /** %1$s - 'class' attribute */     =>'level-' . 0$depth,
                'show_count'/** %2$s - 'value' attribute */   =>  false$category->slug,
                'hide_empty'        =>  false
            );
            wp_dropdown_categories($dropdown_args);
            
        endforeach;
        
    }
    
    /**
     * Change the 'field' key in all tax_query related settings to 'id'
 %3$s - 'selected' attribute */
    public function set_tax_query_field_to_id($query){

        global $typenow;
        
        if(!isset($query$category->tax_query))
            return false;
            
        $types>slug === array_keys($this->cpt$args['selected']);    // Grab the post types that are being edited
            
    ? ' selected="selected"' : if(!in_array($typenow'', $types))     // Check to see if the page now is one of the custom post types supplied by the user
            return false;
        
        $filters/** =%4$s $this->cpt[$typenow];  option text /*/ Grab all of the taxonomies that shold have a filter
        
        foreach($filters as $tax_slug) :
        
            if(isset($query->tax_query->queries)) : foreach($query->tax_query$category->queries as &$single_tax_query) :>name,
            
      /** %5$s - The term count */        if($single_tax_query['taxonomy'] === $tax_slug$args['show_count'])
                        $single_tax_query['field'] = 'id';
                    
                endforeach;
            
            endif;
            
           ? if(isset'&nbsp;&nbsp;($query->tax_query->queried_terms))' :. foreach($query->tax_query$category->queried_terms as $term>count =>. &$settings')' :
            ''
                    if($term === $tax_slug)
                        $settings['field'] = 'id';
                    
                endforeach;
            
            endif;
            
        endforeach;;
        
    }
    
}

So, I had to take the set_tax_query_field_to_id action and amend the tax_query after it had been created.

Here is the full Class that I am using to add filters for my custom taxonomies to my various custom post types -

class FGW_CPT_Taxonomy_Filter{
    
    /**
     * The custom post type to alter and the taxonomies to show filters for
     *
     * @var array
     */
    private $cpt = array();
    
    /**
     * Constructor
     *
     * @param required array $cpt   The custom post type and associated taxonomies to show filters for (as "array($cpt => array(tax1, tax2))")
     */
    public function __construct($cpt = array()){
    
         $this->cpt = $cpt;
        add_action('restrict_manage_posts', array($this, 'render_filter'));
        add_action('parse_tax_query', array($this, 'set_tax_query_field_to_id'));
        
    }
    
     /**
     * Render the taxonomy filter
     */
    public function render_filter(){
    
        global $typenow;
        
        $types = array_keys($this->cpt);    // Grab the post types that are being edited
        
        if(!in_array($typenow, $types))     // Check to see if the page now is one of the custom post types supplied by the user
            return false;
        
        $filters = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
        
        foreach($filters as $tax_slug) :
        
            $tax_obj = get_taxonomy($tax_slug); // Grab the taxonomy object (from the taxonomy slug)
            
            $dropdown_args = array(             // Output the filter dropdown
                'show_option_all'   =>  $tax_obj->labels->all_items,
                'taxonomy'          =>  $tax_slug,
                'name'              =>  $tax_slug,
                'orderby'           =>  'name',
                'selected'          =>  (isset($_GET[$tax_slug]) ? $_GET[strtolower($tax_slug)] : null),
                'hierarchical'      =>  true,
                'depth'             =>  0,
                'show_count'        =>  false,
                'hide_empty'        =>  false
            );
            wp_dropdown_categories($dropdown_args);
            
        endforeach;
        
    }
    
    /**
     * Change the 'field' key in all tax_query related settings to 'id'
     */
    public function set_tax_query_field_to_id($query){

        global $typenow;
        
        if(!isset($query->tax_query))
            return false;
            
        $types = array_keys($this->cpt);    // Grab the post types that are being edited
            
        if(!in_array($typenow, $types))     // Check to see if the page now is one of the custom post types supplied by the user
            return false;
        
        $filters = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
        
        foreach($filters as $tax_slug) :
        
            if(isset($query->tax_query->queries)) : foreach($query->tax_query->queries as &$single_tax_query) :
            
                    if($single_tax_query['taxonomy'] === $tax_slug)
                        $single_tax_query['field'] = 'id';
                    
                endforeach;
            
            endif;
            
            if(isset($query->tax_query->queried_terms)) : foreach($query->tax_query->queried_terms as $term => &$settings) :
            
                    if($term === $tax_slug)
                        $settings['field'] = 'id';
                    
                endforeach;
            
            endif;
            
        endforeach;
        
    }
    
}

So, I had to make a custom walker and use that with the wp_dropdown_categories() function, as below -

/**
 * Custom extended FGW_Cat_Slug_Walker Class
 * Outputs the term 'slug' as the value of each option, not 'term_id' (as is default)
 */
class FGW_Cat_Slug_Walker extends Walker_CategoryDropdown{

    /**
     * Start the element output
     *
     * @param required string $output   Passed by reference. Used to append additional content
     * @param required object $category Category data object
     * @param required int    $depth    Depth of category in reference to parents. Default 0
     * @param required array  $args     An array of arguments
     * @param required int    $id       ID of the current category
     */
    public function start_el(&$output, $category, $depth = 0, $args = array(), $id = 0){
    
        $pad = str_repeat('&nbsp;', $depth * 3);    // Create the padding (before nested terms)
        
        /** Generate the HTML for this option */
        $output.= sprintf("\t".
            '<option class="%1$s" value="%2$s" %3$s>%4$s%5$s</option>',
            /** %1$s - 'class' attribute */     'level-' . $depth,
            /** %2$s - 'value' attribute */     $category->slug,
            /** %3$s - 'selected' attribute */  ($category->slug == $args['selected']) ? ' selected="selected"' : '',
            /** %4$s - option text */           $category->name,
            /** %5$s - The term count */        ($args['show_count']) ? '&nbsp;&nbsp;(' . $category->count . ')' : ''
        );
        
    }
}
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38

Ok, so the answer here is both annoying and fairly difficult to find/fix.

The wp_dropdown_categories() function outputs the dropdown for each custom taxonomy filter using the term_id as the value for each option. However, when constructing the query the WP_Query::parse_tax_query() method, by default, sets the field element to slug.

So, I had to take the set_tax_query_field_to_id action and amend the tax_query after it had been created.

Here is the full Class that I am using to add filters for my custom taxonomies to my various custom post types -

class FGW_CPT_Taxonomy_Filter{
    
    /**
     * The custom post type to alter and the taxonomies to show filters for
     *
     * @var array
     */
    private $cpt = array();
    
    /**
     * Constructor
     *
     * @param required array $cpt   The custom post type and associated taxonomies to show filters for (as "array($cpt => array(tax1, tax2))")
     */
    public function __construct($cpt = array()){
    
        $this->cpt = $cpt;
        add_action('restrict_manage_posts', array($this, 'render_filter'));
        add_action('parse_tax_query', array($this, 'set_tax_query_field_to_id'));
        
    }
    
    /**
     * Render the taxonomy filter
     */
    public function render_filter(){
    
        global $typenow;
        
        $types = array_keys($this->cpt);    // Grab the post types that are being edited
        
        if(!in_array($typenow, $types))     // Check to see if the page now is one of the custom post types supplied by the user
            return false;
        
        $filters = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
        
        foreach($filters as $tax_slug) :
        
            $tax_obj = get_taxonomy($tax_slug); // Grab the taxonomy object (from the taxonomy slug)
            
            $dropdown_args = array(             // Output the filter dropdown
                'show_option_all'   =>  $tax_obj->labels->all_items,
                'taxonomy'          =>  $tax_slug,
                'name'              =>  $tax_slug,
                'orderby'           =>  'name',
                'selected'          =>  (isset($_GET[$tax_slug]) ? $_GET[strtolower($tax_slug)] : null),
                'hierarchical'      =>  true,
                'depth'             =>  0,
                'show_count'        =>  false,
                'hide_empty'        =>  false
            );
            wp_dropdown_categories($dropdown_args);
            
        endforeach;
        
    }
    
    /**
     * Change the 'field' key in all tax_query related settings to 'id'
     */
    public function set_tax_query_field_to_id($query){

        global $typenow;
        
        if(!isset($query->tax_query))
            return false;
            
        $types = array_keys($this->cpt);    // Grab the post types that are being edited
            
        if(!in_array($typenow, $types))     // Check to see if the page now is one of the custom post types supplied by the user
            return false;
        
        $filters = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
        
        foreach($filters as $tax_slug) :
        
            if(isset($query->tax_query->queries)) : foreach($query->tax_query->queries as &$single_tax_query) :
            
                    if($single_tax_query['taxonomy'] === $tax_slug)
                        $single_tax_query['field'] = 'id';
                    
                endforeach;
            
            endif;
            
            if(isset($query->tax_query->queried_terms)) : foreach($query->tax_query->queried_terms as $term => &$settings) :
            
                    if($term === $tax_slug)
                        $settings['field'] = 'id';
                    
                endforeach;
            
            endif;
            
        endforeach;
        
    }
    
}