wc_get_products Function: Arguments and Examples [Guide]

wc_get_products function guide thumbnail with product grid icon

If you have built anything for WooCommerce that lists products, such as a featured block, a custom landing page, or a related-products widget, you have probably reached for wc_get_products. It is the function WooCommerce uses to query its catalog, and it understands product concepts like type, stock status, visibility, and price that a plain WP_Query does not.

This guide explains what the function returns, every argument that matters, and how to use it in real situations: featured product lists, paginated catalogs, ID-only queries, related products, and price filters. The examples target WooCommerce 3.0 and later.

What wc_get_products Does

wc_get_products( $args ) takes an array of query arguments and returns an array of WC_Product objects. Internally it builds a WC_Product_Query, so each result is a fully loaded product. You can call get_name(), get_price(), or is_in_stock() straight away without extra lookups.

Reach for it anywhere you need products outside the standard loop: custom templates, shortcodes, REST callbacks, cron jobs, or admin reports. If you need a single product rather than a list, see the wc_get_product function guide.

Basic Usage

The simplest call looks like this:

<?php

$products = wc_get_products( array(
    'limit' => 5,
) );

foreach ( $products as $product ) {
    echo esc_html( $product->get_name() );
}

Always pass limit explicitly. Leaving it out uses a default page size, and -1 returns every match, which gets slow on large catalogs.

Arguments You Can Pass

Most queries need only a handful of these arguments. The table covers the ones you will use most.

ArgumentAcceptsWhat it does
statusstring or arrayPost status, such as publish, draft, or private. Defaults to publish.
typestring or arraysimple, variable, grouped, or external.
categoryarray of slugsProduct categories. Matches any listed slug.
tagarray of slugsProduct tags. Matches any listed slug.
skustringExact SKU match.
include / excludearray of IDsRestrict the results to, or remove, specific products.
featuredbooltrue returns only featured products.
stock_statusstringinstock, outofstock, or onbackorder.
visibilitystringcatalog, search, visible, or hidden.
orderby / orderstringSort by date, title, price, popularity, rating, menu_order, or id. Order is ASC or DESC.
limit / pageintegerPage size and page number. -1 returns all matches.
paginateboolReturns an object with products, total, and max_num_pages instead of an array.
returnstringobjects (default) or ids.
meta_queryarrayStandard WordPress meta query, useful for custom fields and the _price key.

Tip: Use 'return' => 'ids' when you only need IDs, for example to feed a bulk action. It skips building product objects and is much lighter.

Practical Use Cases

The sections below walk through common situations. Each example is a small, self-contained function you can adapt to your project.

A shortcode that lists up to four featured products, newest first, is a common need for homepages and sidebars.

<?php

function my_featured_products_shortcode() {
    $featured = wc_get_products( array(
        'status'   => 'publish',
        'featured' => true,
        'limit'    => 4,
        'orderby'  => 'date',
        'order'    => 'DESC',
    ) );

    if ( empty( $featured ) ) {
        return '';
    }

    $html = '<ul class="my-featured">';

    foreach ( $featured as $product ) {
        $html .= sprintf(
            '<li><a href="%s">%s</a></li>',
            esc_url( $product->get_permalink() ),
            esc_html( $product->get_name() )
        );
    }

    $html .= '</ul>';

    return $html;
}

add_shortcode( 'my_featured_products', 'my_featured_products_shortcode' );

Add [my_featured_products] wherever you need the list. The output is escaped with esc_url() and esc_html(), which keeps it safe even when product names contain special characters.

[screenshot] WooCommerce product edit screen with the Featured product checkbox ticked in the Publish panel.

Filter by Category and Stock Status

Category and stock filters are useful for “available now” pages. Passing several slugs to category returns products that match any of them, so this example returns shoes or boots that are in stock.

<?php

$products = wc_get_products( array(
    'status'       => 'publish',
    'category'     => array( 'shoes', 'boots' ),
    'stock_status' => 'instock',
    'orderby'      => 'title',
    'order'        => 'ASC',
    'limit'        => 50,
) );

Because stock_status reads the stock state at query time, the list stays accurate without extra checks in your template.

Paginate Large Catalogs

For a custom archive with its own pagination, set paginate to true. The result is an object instead of an array, so loop over products rather than the result itself.

<?php

function my_get_catalog_page( $page = 1, $per_page = 12 ) {
    return wc_get_products( array(
        'status'   => 'publish',
        'limit'    => $per_page,
        'page'     => $page,
        'paginate' => true,
    ) );
}

$result = my_get_catalog_page( 2 );

foreach ( $result->products as $product ) {
    echo esc_html( $product->get_name() );
}

echo esc_html( $result->max_num_pages );

Pass the page number from a query string or route. Because total and max_num_pages come back with the products, you can build pagination links without a second query.

Fetch Only Product IDs

When you only need IDs, for example to build a cache key, run a bulk update, or pass values to a background job, request IDs. This avoids the cost of loading every product object.

<?php

$ids = wc_get_products( array(
    'status' => 'publish',
    'type'   => 'simple',
    'return' => 'ids',
    'limit'  => 100,
) );

Call wc_get_product() on a single ID later if you need the full object.

Exclude the Current Product

On a single product page, related products should not include the product being viewed. Use exclude with the current ID, and build the category list from the product’s own terms. Related products usually render through the hooks covered in the woocommerce_single_product_summary guide.

<?php

function my_related_products( $product, $limit = 4 ) {
    $terms = get_the_terms( $product->get_id(), 'product_cat' );

    if ( ! $terms || is_wp_error( $terms ) ) {
        return array();
    }

    return wc_get_products( array(
        'status'   => 'publish',
        'category' => wp_list_pluck( $terms, 'slug' ),
        'exclude'  => array( $product->get_id() ),
        'orderby'  => 'date',
        'limit'    => $limit,
    ) );
}

The early return handles products with no categories. Without it, an empty category list would drop the category filter entirely and return unrelated products.

Filter by Price in PHP

WooCommerce stores the active price of each product in the _price meta key, which includes sale prices. A standard meta_query on that key filters by price range.

<?php

function my_products_in_price_range( $min, $max ) {
    return wc_get_products( array(
        'status'     => 'publish',
        'orderby'    => 'price',
        'order'      => 'ASC',
        'limit'      => 20,
        'meta_query' => array(
            array(
                'key'     => '_price',
                'value'   => array( $min, $max ),
                'type'    => 'NUMERIC',
                'compare' => 'BETWEEN',
            ),
        ),
    ) );
}

Because the query uses the active price, a product on sale matches its sale price. Test with a few products before relying on this for a customer-facing feature.

Common Mistakes to Avoid

Most problems come from a few habits:

  • Omitting limit. A query without a limit can load far more products than your template needs. Set it on every call.
  • Passing term IDs to category. The argument expects slugs. Passing IDs matches nothing.
  • Treating paginated results as an array. With paginate enabled, the products live under products.
  • Querying on every page load. Product lists rarely change minute to minute. Cache the results with a transient and clear it when a product is saved.
  • Printing values without escaping. Product names and URLs are user-supplied data. Wrap them in esc_html() and esc_url().

FAQs About wc_get_products

Does wc_get_products return draft products?

No. The default status is publish. Pass an array such as array( 'publish', 'draft' ) to include other statuses.

Does it return variations of variable products?

It returns the parent product. Call $product->get_children() on a variable product to get the variation IDs.

Can I use WP_Query instead?

Yes, with post_type => 'product', but you get post objects and must handle product logic yourself. For most product lists, wc_get_products is the better choice.

Which WooCommerce version added it?

WooCommerce 3.0 introduced wc_get_products. Every current WooCommerce install has it.

Conclusion

The wc_get_products function gives you WooCommerce’s catalog logic in one call. Start with a few arguments, add paginate or return when the situation calls for it, and cache the queries that run often.

If you need a custom product listing, a related-products rule, or a checkout filter built on these queries, hire a WooCommerce developer to build it, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.