WooCommerce: Filter By Featured @ Products Admin

Unfortunately, if you still use the “star icon” to feature your WooCommerce products in the admin dashboard, there is no way to “sort by featured” or “filter by featured” in the Products table. If you’ve featured many products, it’s basically impossible to see them all at once, unless you scroll through many pages of products.

Today, we will study how to add a new filter beside the existing ones (“Select a category“, “Filter by product type“, “Filter by stock status“) so that you are able to see all your WooCommerce featured products or – in alternative – all products that are not featured.

In the snippet below, we first add a new select dropdown with the two options, and then we modify the query so that it can listen to the custom GET parameter, and return all featured products or all non-featured products. Enjoy!

Here’s the new dropdown filter under “Products” in the WordPress dashboard. You will be able to select “Featured Only” or “Not Featured”, and on “filter” button click, you will see the relevant results.

PHP Snippet: Add New Filter (“Filter by featured status”) @ WooCommerce Products Admin Table

/**
 * @snippet       Filter by Featured @ WooCommerce Products Admin
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_products_admin_list_table_filters', 'bbloomer_featured_filter' );

function bbloomer_featured_filter( $filters ) {
	$filters['featured_choice'] = 'bbloomer_filter_by_featured';
	return $filters;
}

function bbloomer_filter_by_featured() {
	$current_featured_choice = isset( $_REQUEST['featured_choice'] ) ? wc_clean( wp_unslash( $_REQUEST['featured_choice'] ) ) : false;
	$output = '<select name="featured_choice" id="dropdown_featured_choice"><option value="">Filter by featured status</option>';
	$output .= '<option value="onlyfeatured" ';
	$output .= selected( 'onlyfeatured', $current_featured_choice, false );
	$output .= '>Featured Only</option>';
	$output .= '<option value="notfeatured" ';
	$output .= selected( 'notfeatured', $current_featured_choice, false );
	$output .= '>Not Featured</option>';
	$output .= '</select>';
	echo $output;
}

add_filter( 'parse_query', 'bbloomer_featured_products_query' );

function bbloomer_featured_products_query( $query ) {
    global $typenow;
    if ( $typenow == 'product' ) {
        if ( ! empty( $_GET['featured_choice'] ) ) {
            if ( $_GET['featured_choice'] == 'onlyfeatured' ) {
                $query->query_vars['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field' => 'slug',
                    'terms' => 'featured',
                );
            } elseif ( $_GET['featured_choice'] == 'notfeatured' ) {
                $query->query_vars['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field' => 'slug',
                    'terms' => 'featured',
                    'operator' => 'NOT IN',
                );
            }
        }
    }
    return $query;
} 

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Filter By Featured @ Products Admin

  1. Rodolfo, this code is an absolute winner! I have a client with over 600 products and she needed to see which products were currently featured. Your filter was exactly what I needed to resolve her problem. Thanks so much!

    Regards from Cape Town!
    Ilana

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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