WooCommerce Get All Products


The World's No.1 WooCommerce Plugin

Getting All Products In WooCommerce Using PHP - The Quick and Easy Way

There are many use cases for wanting to collect all products in your database using PHP.

This is actually surprising quick and easy and WooCommerce have created a function that specifically addresses this.

You can also use filters to collect the list of products that you need from the WooCommerce database.

Without further adieu, this is how you get all products from WooCommerce.

Collect A List Of Products With wc_get_products

WooCommerce comes with a native function which safely collects products for you.

This function is called wc_get_products() and is very simple to use.

We’ve listed the default arguments in the code block below so you can modify this to suit your needs.

It will return an array of product objects, so that you can loop through and do what you need to do for each product.


					// Default arguments
$args = array( 
    'status'            => array( 'draft', 'pending', 'private', 'publish' ),  
    'type'              => array_merge( array_keys( wc_get_product_types() ) ),  
    'parent'            => null,  
    'sku'               => '',  
    'category'          => array(),  
    'tag'               => array(),  
    'limit'             => get_option( 'posts_per_page' ),  // -1 for unlimited
    'offset'            => null,  
    'page'              => 1,  
    'include'           => array(),  
    'exclude'           => array(),  
    'orderby'           => 'date',  
    'order'             => 'DESC',  
    'return'            => 'objects',  
    'paginate'          => false,  
    'shipping_class'    => array(),  

// Array of product objects
$products = wc_get_products( $args ) { 

// Loop through list of products 
foreach( $products as $product ) {

    // Collect product variables
    $product_id   = $product->get_id();
    $product_name = $product->get_name();

    // Output product ID and name
    echo 'Product ID: ' . $product_id . ' is "' . $product_name . '"<br>';

    // Do whatever...


The Alternative Way To Get All Products In WooCommerce

You can achieve the same thing by using the WC_Product_Query class.

This actually gets you closer to the source as the wc_get_products() function basically just calls this class anyway. The difference is you can access a few more things in this class than you can with the simple function previously mentioned.

The code below will perform a simple query returning the last 10 products from your database.

You can of course perform quite complex filters on this if you would like,  i’ll write a bit more on filtering below.

					// Set your args
$args = array(  

    'limit' => 10,
    'orderby' => 'date',
    'order' => 'DESC'

// Perform Query
$query = new WC_Product_Query($args);

// Collect Product Object
$products = $query->get_products();

// Loop through products
if ( ! empty( $products ) ) {

    foreach ($products as $product) {

        echo get_permalink( $product->get_id() ) . '<br>';



How To Filter Your Product Query On The WooCommerce Database

Ultimately, you can filter your product query in anyway that you could possibly imagine.

There is a lot supported out of the box, but if your ideal filters aren’t available you can use a php filter to modify the product query filters.

The out of the box filters are handled by modifying the $args section that you can find in each of the blocks of code above.

To get a comprehensive list of all of the out-of-the-box filtering options I would recommend checking out this article on WooCommerce Product Query Filters.

Creating Custom Filters To Get All Products In WooCommerce

As mentioned, you can handle fairly complex queries with the out-of-the-box functionality provided by the WC_Product_Query class.

However, if you have added custom fields or have some other custom elements to your website you may not be able to query these easily using the supported args.

This is where the woocommerce_product_data_store_cpt_get_products_query filter comes into play.

This filter allows you to tap into anything found in the wp_postmeta table (ideal for custom fields).

If you copy paste the below code into your functions.php this will start you off with creating a custom filter.

You just need to make sure that you update the meta_key according to the meta_key you are trying to query in your database.

 *  Create custom filter for WC_Product_query
 *  Add this to functions.php
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'wpd_custom_query_var', 10, 2 );
function wpd_custom_query_var( $query, $query_vars ) {

    $meta_key = '_custom_price';

    if ( ! empty( $query_vars['customvar'] ) ) {

        $query['meta_query'][] = array(
            'key'       => $meta_key,
            'value'     => esc_attr( $query_vars[$meta_key] ),
            'compare'   => '>', // <=== Here you can set other comparison arguments



    return $query;

// How to use the new filter
$products = wc_get_products( array( '_custom_price' => '20' ) ); // Search for products with _custom_price greater than 20 (due to our comparison filter)

Need Super Precise Database Queries For Getting Product Data?

The last way you might choose to access the products in your database would be through WP_Query.

This is the original WordPress query class and offers the most flexibility and functionality.

WooCommerce products are after all just posts in your database, so you can query posts with the type “product” and this would give you all of the features of WP_Query.

The drawback is that it’s not designed specifically for querying products so you need to know what you’re doing, and it’s also possible that WooCommerce will change the way they deal with product data.

THis means that it wouldn’t be a future-proof way of handling things, or the easiest way of handling things.

But it is certainly the most advanced method and would give you the most options.

Notify of
Inline Feedbacks
View all comments