WooCommerce developers and store owners often need to programmatically fetch all products from a WooCommerce store — whether for integrations, data analysis, custom displays, or API-driven applications. This comprehensive guide explains the most effective techniques for retrieving all products using WordPress PHP functions and the WooCommerce REST API. You’ll learn practical code snippets, best practices, pagination handling, variations inclusion, real examples, and troubleshooting tips for large product catalogs. By the end of this article you’ll have a clear, step-by-step understanding of how to fetch every product in your store reliably and efficiently.
Introduction to WooCommerce Product Retrieval
WooCommerce stores products as custom post types within WordPress, and there are multiple ways to programmatically access this data. Two common methods are:
- Using PHP functions such as wc_get_products() directly within your WordPress theme or plugin code.
- Using the WooCommerce REST API to fetch product data remotely, ideal for integrations and external apps.
Each method serves different use cases. The PHP approach is faster and ideal for server-side theme or plugin operations. The REST API approach is optimal when accessing product data from external systems like mobile apps or integration services. Both approaches require understanding pagination and query parameters to retrieve complete datasets, especially when dealing with stores that have hundreds or thousands of products.
1. Using wc_get_products() to Retrieve All Products in WooCommerce
WooCommerce provides a function called wc_get_products() that allows developers to query product collections using an array of arguments. This function is recommended over raw database queries or WP_Query because it’s future-proof and compatible with WooCommerce data structures.
1.1 Basic Usage of wc_get_products()
The simplest example to fetch all products looks like this:
<?php
$args = array(‘limit’ => -1);
$products = wc_get_products($args);
foreach ($products as $product) {
echo $product->get_name();
}
Explanation:
- ‘limit’ => -1 tells WooCommerce to return all products without limiting the count.
- This code loops through each product object and displays its name.
This approach reliably fetches every product without pagination if your server can handle it. If product count is extremely large, consider using pagination for memory efficiency.
1.2 Fetching Only Published Products
Often you only want products visible on the storefront:
$args = array( ‘limit’ => -1, ‘status’ => ‘publish’ ); $products = wc_get_products($args);
This ensures only published products are returned, excluding drafts and private products that aren’t visible to customers.
1.3 Including Pagination for Large Stores
If your store has thousands of products, fetching everything at once may result in memory issues. Instead, use the pagination feature.
$args = array( ‘limit’ => 100, ‘paginate’ => true ); $products_page = wc_get_products($args);
foreach ($products_page->products as $product) {
echo $product->get_name();
}
This returns 100 product records at a time, allowing you to loop through pages and execute logic page by page.
1.4 Fetching Products by Category, SKU or Type
You can customize your product queries further using additional arguments:
- By Category: Specify category slugs in an array to filter products within those categories.
- By SKU: Query product SKUs directly for targeted retrieval.
- By Type: Fetch only simple, variable, or grouped products.
$args = array( ‘category’ => array(‘electronics’,’books’), ‘limit’ => -1 ); $products = wc_get_products($args);
You can adjust these query parameters depending on your needs. Always test query outcomes in staging environments.
2. Using WooCommerce REST API to Get All Products
The WooCommerce REST API allows fetching store products from remote systems using HTTP requests. This method is essential for mobile apps, external dashboards, integration connectors, or any third-party system needing product data without direct database access.
2.1 Understanding REST API Endpoints
WooCommerce defines endpoints such as:
- /wp-json/wc/v3/products – Returns product objects.
- /wp-json/wc/v3/products?per_page=100 – Limits results per page.
Important: The REST API defaults to pagination with a maximum per_page of 100. To retrieve more products, you must loop through multiple pages.
2.2 Fetching Products with REST API
Here’s a basic example using curl:
curl “https://yourstore.com/wp-json/wc/v3/products?per_page=100&status=publish” -u consumer_key:consumer_secret
Explanation:
- per_page=100 retrieves up to 100 products at once.
- By looping page numbers and aggregating data, you can build a complete list of all products.
2.3 Including Product Variations
The REST API’s products endpoint returns parent products by default. To retrieve variations associated with variable products, you should use the variations endpoint. For each variable parent product, make additional calls like:
GET /wp-json/wc/v3/products/{parent_id}/variations
This ensures you fetch not only parent products but also individual variation details such as size, color, and stock variations.
3. Best Practices When Fetching WooCommerce Products
3.1 Avoid Raw Database Queries
WooCommerce’s internal data structures are subject to change. Functions like wc_get_products() and official REST API endpoints abstract underlying table changes and ensure compatibility with future versions.
3.2 Handle Pagination Efficiently
For large catalogs, always design your logic to handle pages. Avoid fetching thousands of products at once if your server resources are limited.
3.3 Respect Store Performance
If you’re running product queries on a live store, consider scheduling heavy operations during off-peak times or using background processing to reduce performance impact.
Pro Tips for Fetching All WooCommerce Products
- Use caching layers: When fetching product lists repeatedly, locally cache results to reduce repeated API calls or database load.
- Use secure API authentication: Always use generated Consumer Key/Secret with HTTPS to protect sensitive store data.
- Consider incremental syncing: When fetching large datasets from REST API, use time-based filters to only retrieve updated products.
- Test on staging: Always validate your code in staging environments before deploying to production.
- Use proper error handling: Handle connection errors, timeouts, and rate limits gracefully.
- Leverage WordPress hooks: If outputting product data on frontend templates, use appropriate hooks to avoid disrupting existing store functionality.
Frequently Asked Questions
Can wc_get_products retrieve product variations?
Yes — simply include the ‘type’ => ‘variation’ argument. However, for complete variation data, REST API variation endpoints are more suited, especially for external systems. By default broad queries return parent products.
Why does the REST API return only 100 products at a time?
The WooCommerce REST API uses pagination to optimize performance. The maximum allowable value for per_page is 100 — beyond which you must use pagination and loop through pages with incremental page parameters until all products are collected.
What happens if my store has thousands of products?
Never fetch all products in a single call in memory-constrained environments. Use pagination and background processing to load products in chunks. Consider storing results incrementally to avoid performance degradation.
Do I need authentication for REST API calls?
Yes — unless your endpoint is public. For most store data, you should authenticate using WooCommerce API keys (Consumer Key and Consumer Secret) over HTTPS to secure access.
Is there a limit to how many products wc_get_products can fetch?
No inherent hard limit — setting ‘limit’ => -1 returns all products. However, PHP memory limits can impose practical limits. In such cases use ‘paginate’ => true and loop through pages.
Can I fetch products by custom metadata?
Yes — by specifying appropriate query arguments in wc_get_products() you can filter products based on metadata fields, SKU, categories, or custom taxonomies.
Conclusion
Retrieving all products from a WooCommerce store is a common but potentially complex task depending on the volume of products and the method used. Understanding how to use wc_get_products() efficiently within WordPress or leveraging the WooCommerce REST API with proper pagination allows you to build reliable, scalable solutions for data integrations, backend systems, custom displays, and automated workflows. By following best practices and using the right approach for your scenario, you can ensure accurate, performant product retrieval that supports your store’s technical needs.







