- What Is the WC_Product_Query Object?
- How to Query Products with WC_Product_Query
- Common Query Variables
- 6 Practical WC_Product_Query Use-Cases
- 1. Featured Products on a Landing Page
- 2. In-Stock Products in One Category
- 3. Paginated Product Catalog
- 4. Fast ID Lists for Bulk Operations
- 5. Related Products by Tag
- 6. Product Reports and Exports
- Modifying Queries with Hooks
- FAQs About WC_Product_Query
- How is WC_Product_Query different from WP_Query?
- Does it return product variations?
- Can I use it in a shortcode?
- How do I get the total count cheaply?
- Conclusion
Need a short list of featured products for a landing page, or a category that hides sold-out items? You can write a raw WP_Query against the product post type, but then you have to handle product types, stock rules, and pagination yourself.
That is what the WC_Product_Query Object is for. It gives you a clean, object-oriented way to fetch WooCommerce products using named arguments. This guide covers what the class does, the query variables you will use most, and six use-cases you can paste into a plugin or child theme.
What Is the WC_Product_Query Object?
WC_Product_Query is a product query class in WooCommerce. It extends WC_Object_Query, so it follows the same pattern as other WooCommerce object queries, and it returns WC_Product objects. You create an instance with an array of arguments and call get_products() to run the query.
Because the class is dedicated to products, your code reads clearly. You can also adjust its arguments with a standard WordPress hook before the query runs.
How to Query Products with WC_Product_Query
Creating a Query
Pass an array of query variables to the constructor, then call get_products(). Any variable you leave out uses the class default.
<?php
$query = new WC_Product_Query(
array(
'status' => 'publish',
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
)
);
$products = $query->get_products();
$names = array();
foreach ( $products as $product ) {
$names[] = $product->get_name();
}Reading the Results
get_products() returns an array of WC_Product objects, so you can use standard getters such as get_name(), get_price(), and get_permalink(). To get plain IDs instead, set the return variable to ids (see use-case 4 below).
If you prefer a function-style call, the wc_get_products() function guide covers the same arguments in a shorter form.
Common Query Variables
These are the variables you will use most. Each one maps to a WooCommerce product field or a WordPress pagination setting.
| Variable | Purpose |
|---|---|
status | Post status, such as publish or draft |
type | Product type: simple, variable, grouped, or external |
limit | Number of products to return. Use -1 for all. |
category | Array of category slugs |
tag | Array of tag slugs |
featured | Set to true to return featured products only |
stock_status | instock, outofstock, or onbackorder |
orderby | Sort field, such as date, price, or title |
order | ASC or DESC |
return | objects (default) or ids |
paginate | Set to true to return an object with products, total, and max_num_pages |
6 Practical WC_Product_Query Use-Cases
Wrap each query in a small function that returns the data your template needs. These six patterns cover the most common needs.
1. Featured Products on a Landing Page
Use featured to build a short list of highlighted products for a homepage section or landing page.
<?php
function my_featured_products() {
$query = new WC_Product_Query(
array(
'featured' => true,
'limit' => 4,
'status' => 'publish',
)
);
return $query->get_products();
}Mark a product as featured under Catalog visibility on the product edit screen.
[screenshot] WooCommerce product edit screen with the Featured product checkbox ticked in the Catalog visibility box.
2. In-Stock Products in One Category
Combine category with stock_status to hide sold-out items from a category archive or a “shop this collection” block.
<?php
$products = ( new WC_Product_Query(
array(
'category' => array( 'shoes' ),
'stock_status' => 'instock',
'limit' => 50,
)
) )->get_products();3. Paginated Product Catalog
Set paginate to true and read the page number from the request. The result object gives you the products and the counts you need to build page links without a second query.
<?php
$page = max( 1, absint( get_query_var( 'paged' ) ) );
$result = ( new WC_Product_Query(
array(
'limit' => 12,
'page' => $page,
'paginate' => true,
)
) )->get_products();
// $result->products, $result->total, $result->max_num_pages4. Fast ID Lists for Bulk Operations
When you only need IDs, for example to feed a bulk-update script or a cache warm-up job, skip the full objects.
<?php
$product_ids = ( new WC_Product_Query(
array(
'limit' => 100,
'return' => 'ids',
)
) )->get_products();Fewer objects means lower memory use and faster runs on large catalogs.
5. Related Products by Tag
Use the current product’s tags to find related items. Exclude the current product so it never lists itself.
<?php
function my_related_products( $product_id ) {
$tags = wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'slugs' ) );
$query = new WC_Product_Query(
array(
'tag' => $tags,
'exclude' => array( $product_id ),
'limit' => 4,
)
);
return $query->get_products();
}6. Product Reports and Exports
For a CSV export or an admin report, query by product type and status, then collect the values you need.
<?php
function my_product_skus() {
$products = ( new WC_Product_Query(
array(
'type' => 'variable',
'status' => 'publish',
'limit' => 500,
)
) )->get_products();
$skus = array();
foreach ( $products as $product ) {
$skus[] = $product->get_sku();
}
return $skus;
}Modifying Queries with Hooks
Two filters let you change the arguments and results of every product query that runs through this class. This is useful when you cannot edit the code that makes the query. woocommerce_product_object_query_args receives the query variables before the query runs. woocommerce_product_object_query receives the results after it runs.
<?php
add_filter( 'woocommerce_product_object_query_args', 'my_simple_products_only' );
function my_simple_products_only( $args ) {
$args['type'] = 'simple';
return $args;
}Tip: Filters run for every WC_Product_Query call, including calls from other plugins. Add a condition inside your filter, such as a check for a specific context, so you do not change every product query on the site.
FAQs About WC_Product_Query
How is WC_Product_Query different from WP_Query?
WP_Query works with any post type and returns post objects. WC_Product_Query understands WooCommerce product types and stock status, and it returns WC_Product objects with product getters ready to use. Use it for products and WP_Query for everything else.
Does it return product variations?
Not by default. Setting type to variable returns parent products. To reach a variation, call get_children() on the parent product.
Can I use it in a shortcode?
Yes. Run the query inside a callback that returns HTML, and register that callback with add_shortcode(). Keep the markup in a template file so the PHP stays readable.
How do I get the total count cheaply?
Set paginate to true and read total from the result. You get the count without a separate count query.
Conclusion
WC_Product_Query is a clean, supported way to fetch WooCommerce products in PHP. Start with the basic query, then add the variables your layout needs.
Need a custom product listing or a filtered catalog? Hire an expert WooCommerce developer, or reach out to me directly with your project.
