wc_get_products

Products wrapper for get_posts.

Description

(array|stdClass) wc_get_products( (array) $args ); 

This function should be used for product retrieval so that we have a data agnostic way to get a list of products.

Args: status array|string List of statuses to find. Default: any. Options: any, draft, pending, private and publish. type array|string Product type, e.g. Default: all. Options: all, simple, external, variable, variation, grouped. parent int post/product parent sku string Limit result set to products with specific SKU. category array Limit result set to products assigned to specific categories by slug e.g. array('hoodie', cap,, t-shirt). tag array Limit result set to products assigned to specific tags (by slug) e.g. array('funky', retro , designer) shipping_class array Limit results set to products in specific shipping classes (by slug) e.g. array('standard', next-day) limit int Maximum of products to retrieve. offset int Offset of products to retrieve. page int Page of products to retrieve. Ignored when using the offset arg. exclude array Product IDs to exclude from the query. orderby string Order by date, title, id, modified, rand etc order string ASC or DESC return string Type of data to return. Allowed values: ids array of Product ids objects array of product objects (default) paginate bool If true, the return value will be an array with values: products => array of data (return value above), total => total number of products matching the query max_num_pages => max number of pages found

Returns (array|stdClass)

Number of pages and an array of product objects if paginate is true, or just an array of values.

Parameters (1)

0. $args (array)
Array of args (above)

Usage

  1. if ( !function_exists( 'wc_get_products' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // Array of args (above) 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_get_products($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_get_products( $args ) { 
  2. $args = wp_parse_args( $args, array( 
  3. 'status' => array( 'draft', 'pending', 'private', 'publish' ),  
  4. 'type' => array_merge( array_keys( wc_get_product_types() ) ),  
  5. 'parent' => null,  
  6. 'sku' => '',  
  7. 'category' => array(),  
  8. 'tag' => array(),  
  9. 'limit' => get_option( 'posts_per_page' ),  
  10. 'offset' => null,  
  11. 'page' => 1,  
  12. 'include' => array(),  
  13. 'exclude' => array(),  
  14. 'orderby' => 'date',  
  15. 'order' => 'DESC',  
  16. 'return' => 'objects',  
  17. 'paginate' => false,  
  18. 'shipping_class' => array(),  
  19. ) ); 
  20.  
  21. // Handle some BW compatibility arg names wherewp_queryargs differ in naming. 
  22. $map_legacy = array( 
  23. 'numberposts' => 'limit',  
  24. 'post_status' => 'status',  
  25. 'post_parent' => 'parent',  
  26. 'posts_per_page' => 'limit',  
  27. 'paged' => 'page',  
  28. ); 
  29.  
  30. foreach ( $map_legacy as $from => $to ) { 
  31. if ( isset( $args[ $from ] ) ) { 
  32. $args[ $to ] = $args[ $from ]; 
  33.  
  34. return WC_Data_Store::load( 'product' )->get_products( $args );