Vannkorn

Full Stack Web Developer

Close

WordPress Admin: Filter By Taxonomy

Code snippet to help you add a taxonomy filter option in the posts page of your WordPress backend.

Ads: Register now via this link to receive $300 credit from Vultr

Filter by taxonomy options is crucial when you have to categorize your posts into many types and you are looking to modify them or looking for which one of them more quickly than the default behavior.

It is also useful if you’re using WordPress as a headless CMS, or a traditional Database Management System where you can filter for a specific post faster and easier.

To do this, make sure you’ve created taxonomies and assigned some posts to them (for testing purposes).

What you have to do is to create a custom function and import that function into your main functions.php file.

Creat a file called inc.filter-by-taxonomy.php and store in the inc folder on your theme root directory, and then add the following code:

add_action('restrict_manage_posts', 'admin_products_by_manufacturer_filter_dropdown');
function admin_products_by_manufacturer_filter_dropdown() {
    global $typenow, $pagenow;

    $taxonomy = 'product-brands'; // The custom taxonomy

    if( 'edit.php' === $pagenow && 'product' === $typenow && taxonomy_exists( $taxonomy ) ) {
        $info_taxonomy = get_taxonomy($taxonomy);
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';

        wp_dropdown_categories( array(
            'show_option_all' => sprintf( __("Select a brand", "woocommerce"), $info_taxonomy->label ),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'selected'        => $selected,
            'orderby'         => 'name',
            'show_count'      => true,
            'hide_empty'      => true,
        ) );
    }
}

add_action('parse_query', 'admin_products_by_manufacturer_filter_query');
function admin_products_by_manufacturer_filter_query( $query ) {
    global $typenow, $pagenow;

    $taxonomy = 'product-brands'; // The custom taxonomy

    if ( 'edit.php' === $pagenow && 'product' === $typenow && taxonomy_exists( $taxonomy ) ) {
        $q_vars = &$query->query_vars;

        if ( isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
            $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
            $q_vars[$taxonomy] = $term->slug;
        }
    }
}

Include in your main functions.php file like below:

require_once( get_template_directory() . '/inc/inc.filter-by-brand.php');

Or if you’re using a child theme, add the following code into your main functions.php like this:

require_once( get_stylesheet_directory() . '/inc/inc.filter-by-brand.php');

Please note that we use get_stylesheet_directory() to refer to the root location of your child theme instead of get_template_directory() that refers to the root location of your main theme.

Leave a Reply

Your email address will not be published. Required fields are marked *