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.

https://gist.github.com/DeveloperWil/2d1de8a3f1cf5d58db33606431e1a1bd

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