WooCommerce: Add filtering by featured products

The other day someone asked me if they could filter by featured products in the WooCommerce listings, and after doing some tests with slugs I realized that i could not.

It is true that I had never needed it or any customer had asked me for it, but the reality is that it is something that cannot be done(Spoiler: You can).

But of course, WooCommerce is WordPress, open source, and in reality you can do everything, you just have to find the right code, and the code exists.

So if you want, for whatever reason, to add a drop-down menu to filter products to show only the highlighted ones, you must add the following code to your WooCommerce customization plugin, or in its absence at the end of the functions.php file of the active theme:

/* Add filtering by featured products */
add_action('restrict_manage_posts', 'featured_products_sorting');
function featured_products_sorting() {
    global $typenow;
    $post_type = 'product'; // You can change this if it is for other type of content
    $taxonomy  = 'product_visibility'; // Change to your taxonomy
    if ($typenow == $post_type) {
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
        $info_taxonomy = get_taxonomy($taxonomy);
            'show_option_all' => __("Show all {$info_taxonomy->label}"),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'orderby'         => 'name',
            'selected'        => $selected,
            'show_count'      => true,
            'hide_empty'      => true,
add_filter('parse_query', 'featured_products_sorting_query');
function featured_products_sorting_query($query) {
    global $pagenow;
    $post_type = 'product'; // You can change this if it is for other type of content
    $taxonomy  = 'product_visibility'; // Change to your taxonomy
    $q_vars    = &$query->query_vars;
    if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && 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;
}Code language: PHP (php)

If you go through the product list you will see a new drop-down menu where you can filter by featured products and have only these shown:

So here it is, and as you see, of course you can.

Read this post in Spanish: WooCommerce: Añadir filtrado por productos destacados

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

4 thoughts on “WooCommerce: Add filtering by featured products”

  1. This is incredible, thank you so much for taking the time to provide this snippet. It really threw a spanner in the works for us when the ability to click the star was removed and we hadn’t found a solution since. This code worked perfectly, no edits required!

  2. Man you are AMAZING! Thank you so much

    P.S Can you also help with the ‘related products’ that show on a product page? Right now they are not related at all (I have very broad categories). Can we make it better

Leave a Comment

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

Scroll to Top