How To Display WooCommerce Featured Products Without a Shortcode

June 22nd, 2013 - Posted by Steve Marks to MySQL, PHP, Web Development.

WooCommerce comes with a number of useful shortcodes to allow quick insertion of WooCommerce related content within posts and pages. One of these shortcodes can be used to display featured products like so:

[featured_products per_page="12" columns="4"]

The above would output the featured products in a grid.

This is great, however, what if we want go one step further and have more control over the products shown, or if we’re already using the shortcode elsewhere on our site?

Specifying Featured Products in WordPress

First, let’s take a look at how we set featured products. When logged into WordPress simply navigate to the ‘Products’ section and you’ll notice that there is a column containing stars.


If a star in this column is black it means it is featured. To add or remove a product to the list of featured products you simply click the star.


Obtaining The Featured Products

We can obtain the products in question much like we can obtain posts normally by using the WP_Query class like so:

$args = array(
	'post_type' => 'product',
	'meta_key' => '_featured',
	'meta_value' => 'yes',
	'posts_per_page' => 1

$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) : 

	while ($featured_query->have_posts()) : 
		$product = get_product( $featured_query->post->ID );
		// Output product information here

wp_reset_query(); // Remember to reset

The first thing that is happening above is to set the arguments for the query we want to run. You’ll notice we are only getting posts where the post_type is ‘product’, and where the meta_value, ‘_featured’, is set to ‘yes’. We’ve also specified that we only want to get one featured product so simply increase this number if you want to obtain more.

The next part is pretty standard in terms of looping through posts. The only other important line is:

$product = get_product( $featured_query->post->ID );

By doing this we can then access all the product related information, for example SKU, that isn’t available in a normal post object.

This entry was posted on Saturday, June 22nd, 2013 at 10:38 am by +Steve Marks and is filed under MySQL, PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (11)
  1. @Rahul – Glad it helped. The $featured_query->post->ID bit is just a way to get the ID of the product/post in the WordPress loop. You could’ve also used get_the_ID() or $post->ID. All do the same thing.

  2. Thank you for this great tutorial. It is exactly worked how I wanted. But I did not understand about this variable-
    $product = get_product( $featured_query->post->ID );. Can you explain it?

    Thank you

  3. @Tingo – You just put it where you want the products to be displayed. So if you want them on your homepage you do it in your homepage file. If you want them in your sidebar, you do them in a sidebar. Wherever you put the code is where the products will be displayed. Hope that helps.

  4. Tingo says:

    No one is fuc**king decent enough to tell where the fu*k this code will go? there are some people in the world who needs things in bit more detail…. Sorry this is frustration, i am only angry on wooCommerce…..KHelp please? where to put this code to display products on home page?

  5. Fahad Tariq says:

    Bundle of thanks saved my lot of time.

  6. Jesse says:

    You the SHIT man!!!!

  7. SteVader says:

    Stunning! Thanks so much for this, it’s certainly saved me a chunk of frustration.

  8. Andy says:

    Thanks for this… the Woocommerce documentation is nasty.

  9. @Abid – You put this code wherever you want the featured products to appear.

  10. Abid says:

    Where to put these codes?

  11. Satyam Mishra says:

    excellent!!! Thanks you