WooCommerce: Display Featured Products First @ Shop, Cat, Tag, Search Pages

What’s the point of having the chance to “star” some products in your WooCommerce backend, if you can’t really use these featured products properly on the frontend? Yes, you can use a shortcode or a block to return the featured products only… or maybe you want to keep reading for a better (imho) solution!

Highlighting key products can significantly enhance user experience, driving sales by capturing the attention of potential buyers right from the start.

So, if you’re managing a WooCommerce shop, prioritizing featured products can help create a curated shopping experience that emphasizes your best offerings. In this tutorial, I’ll walk you through a simple yet effective code snippet that will help you display featured products first on various pages of your WooCommerce store.

This customization not only boosts visibility for selected items but also encourages shoppers to explore and engage with your inventory. Follow along to optimize your product display and maximize your store’s potential!

PHP Snippet: Display Featured Products First on WooCommerce Shop, Category, Tag, and Search Pages

A few notes before we take a look at the code.

First, the most obvious one – you need to have at least one featured product! Go to “Products” and star the product/s you wish to show first:

If you have many featured products, you can add a backend filter to see them all with this snippet.

Also, this works for “classic” WooCommerce, and it’s not compatible with blocks yet.

Finally, we’ve already seen a way to “sort by featured” in the frontend, but in this case I want to actually show them first in an existing product loop, as long as the products belong to that shop / category / tag / search result page.

/**
 * @snippet       Featured Products First @ Woo Product Loop
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'posts_orderby', 'bbloomer_woocommerce_sticky_products_first', 9999, 2 );
 
function bbloomer_woocommerce_sticky_products_first( $order_by, $query ) {
	global $wpdb;
	if ( ! is_admin() && $query->is_main_query() && is_woocommerce() ) {
		$sticky_product_ids = wc_get_featured_product_ids();
		$order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $sticky_product_ids ) . "') DESC, " . $order_by;
	}
	return $order_by;
}

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

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 *