0

I have a custom post type called Listings. The listings has taxonomies which I want to use to filter down the results when viewing all of the listings in the admin area.

To do this I tried to use the following:

function my_restrict_manage_posts() {
    global $typenow;
    $args = array('public' => true, '_builtin' => false);
    $post_types = get_post_types($args);
    if (in_array($typenow, $post_types)) {
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $tax_obj = get_taxonomy($tax_slug);
            wp_dropdown_categories(array(
                'show_option_all' => __('Show All ' . $tax_obj->label),
                'taxonomy' => $tax_slug,
                'name' => $tax_obj->name,
                'orderby' => 'term_order',
                'selected' => $_GET[$tax_obj->query_var],
                'hierarchical' => $tax_obj->hierarchical,
                'show_count' => false,
                'hide_empty' => true
            ));
        }
    }
}

function my_convert_restrict($query) {
    global $pagenow;
    global $typenow;
    if ($pagenow == 'edit.php') {
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $var = & $query->query_vars[$tax_slug];
            if (isset($var)) {
                $term = get_term_by('id', $var, $tax_slug);
                $var = $term->slug;
            }
        }
    }
}

add_action('restrict_manage_posts', 'my_restrict_manage_posts');
add_filter('parse_query', 'my_convert_restrict');

I am then able to choose the option in the drop down and then press filter and I get a URL like /wp-admin/edit.php?s&post_status=all&post_type=listings&action=-1&m=0&make=149&filter_action=Filter&paged=1&action2=-1

So the ID is being passed but then when the page reloads, I still have all of the listings and the filter hasn't worked. Any suggestions if there is anything I have missed or if there are any errors?

0

I have realised the problem isn't with the code I have posted and the issue is when the taxonomies are being created.

Basically the reason this wasn't working is because the query_var parameter was set to false when registering the taxonomy. I changed this to true and my filtering started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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