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:

%MINIFYHTMLf3c0c2338318e771bcbe56b55fea965f52%
/* 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); wp_dropdown_categories(array( '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; } }

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

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment

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

Scroll to Top Skip to content