0

I have several custom post types, each with one or more custom taxonomies. I'd like to add a filter for each taxonomy to the relevant custom post type edit screen, but my solution is not working.


The problem

First off, I know why the query is failing, but I don't know what is causing the query to be constructed in this manner.

When I inspect the $wp_global query after filtering a post type I notice that the condition AND (1=0) exists, whcih of course is never going to be true.


My (broken) solution

To output the various filters I use a class which can be found in this Pastebin.

To initiate the Class I'm using this code (trimmed down here for just one post type and taxonomy, whcih I will refer to for ease during the rest of this question) -

add_action('init', 'fgw_filter_taxonomies');
function fgw_filter_taxonomies(){

    new FGW_CPT_Taxonomy_Filter(
        array('file' => array('file_type'))
    );
    
}

The filters are displayed perfectly, however when using any of them WP returns no results.


My investigation

I have studied the resultant $wp_query global and I notice that WP is actually adding [file_type] => 22 to the query and query_var arrays, and the tax_query array appears to be created correctly.

However, as previously mentioned, WP is for some reason constructing the query incorrectly, meaning that no results are found.

[tax_query] => WP_Tax_Query Object
    (
        [queries] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => file_type
                        [terms] => Array
                            (
                                [0] => 22
                            )

                        [field] => slug
                        [operator] => IN
                        [include_children] => 1
                    )

            )

        [relation] => AND
        [table_aliases:protected] => Array
            (
            )

        [queried_terms] => Array
            (
                [file_type] => Array
                    (
                        [terms] => Array
                            (
                                [0] => 22
                            )

                        [field] => slug
                    )

            )

        [primary_table] => fgw_posts
        [primary_id_column] => ID
    )

My question

What next? Where can I look to fix this problem? I've trawled through query.php, but I'm nore really sure what I'm looking for.

I'm genuinly stumped by this behaviour, particularly as comparing the resultant $wp_query global to that generated when filtering the builtin category or post_tag texonomy seems identical (other than the name of the taxonomy of course).

1 Answer 1

0

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 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(' ', $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 . ')' : ''
        );

    }
}

Your Answer

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

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