WooCommerce: Return An Array Of Featured Product IDs

If you need to show WooCommerce featured products on non-shop pages, here’s a quick way of grabbing an array of all featured product IDs.

The code below will produce an array of the IDs for all your WooCommerce featured products.

You can then loop through the array and display products on any page in your theme.

The example usage code block above declares a WC_Product_Factory object, gets a list of the featured product IDs then loops through each ID creating a WC_Product object where you can get all the product information from.

See a list of WC_Product API properties and methods at https://woocommerce.github.io/code-reference/classes/WC-Product.html.

Where To Put The Code Snippet?

This code snippet should go in your active theme’s functions.php file.

Add it to the bottom of the file, before any closing ?> characters.

It’s always a good idea to take a backup of your functions.php file in case anything goes wrong – you can just copy the old code back in again.

Was this article helpful?
YesNo

Keep In Touch

Wil

Wil is a dad, WordPress consultant, WordPress developer, business coach and mentor. He co-organizes the WordPress Sydney meetup group and has been on the organising committee for WordCamp Sydney since 2014. He speaks at many special events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.