1. Home
  2. Knowledge Base
  3. WooCommerce Product Table
  4. Advanced Usage

Code snippet: Change the options in a product table filter dropdown

Please note: this article is aimed at developers. We have provided an article on how to add code snippets. If you're not comfortable using them then you can use our plugin customization service.

WooCommerce Product Table automatically lists relevant terms in its filter dropdowns. For example, the categories filter lists product categories which match products in the table.

If you want to remove specific options from a filter dropdown, or show them in a custom order, then you can use a code snippet.

Remove categories from the filter dropdown

Add this PHP code to your theme or child theme, or with a code snippets plugin:

add_filter( 'wc_product_table_search_filter_terms_categories', 'barn2_wpt_remove_category_filter_options' );

function barn2_wpt_remove_category_filter_options( $terms ) {
    $excluded_slugs = [ 'clothing', 'accessories' ];

    return array_values(
        array_filter(
            $terms,
            function( $term ) use ( $excluded_slugs ) {
                return ! in_array( $term->slug, $excluded_slugs, true );
            }
        )
    );
}

Replace clothing and accessories with the slugs of the categories that you want to remove from the filter dropdown.

If you are already excluding categories from the table using the Exclude category option, then WooCommerce Product Table will automatically remove those categories and their child categories from the category filter dropdown.

Reorder category filter options

Use this snippet to list category filter options in a specific order:

add_filter( 'wc_product_table_search_filter_terms_categories', 'barn2_wpt_reorder_category_filter_options' );

function barn2_wpt_reorder_category_filter_options( $terms ) {
    $ordered_slugs = [ 'featured', 'clothing', 'accessories' ];

    usort(
        $terms,
        function( $a, $b ) use ( $ordered_slugs ) {
            $a_position = array_search( $a->slug, $ordered_slugs, true );
            $b_position = array_search( $b->slug, $ordered_slugs, true );

            $a_position = false === $a_position ? PHP_INT_MAX : $a_position;
            $b_position = false === $b_position ? PHP_INT_MAX : $b_position;

            return $a_position <=> $b_position;
        }
    );

    return $terms;
}

Add your category slugs to the $ordered_slugs array in the order you want them to appear. Any category that is not listed will appear after the categories in the snippet.

Other filter dropdowns

The examples above change the categories filter dropdown. To customize a different filter dropdown, change the filter name in the snippet. For example, use wc_product_table_search_filter_terms_tags for the tags filter.

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.