wc_get_products()WC 3.0.0

Standard way of retrieving products based on certain parameters.

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

Args and usage: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

No Hooks.

Return

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

Usage

wc_get_products( $args );
$args(array) (required)
Array of args (above).

Changelog

Since 3.0.0 Introduced.

wc_get_products() code WC 8.7.0

function wc_get_products( $args ) {
	// Handle some BW compatibility arg names where wp_query args differ in naming.
	$map_legacy = array(
		'numberposts'    => 'limit',
		'post_status'    => 'status',
		'post_parent'    => 'parent',
		'posts_per_page' => 'limit',
		'paged'          => 'page',
	);

	foreach ( $map_legacy as $from => $to ) {
		if ( isset( $args[ $from ] ) ) {
			$args[ $to ] = $args[ $from ];
		}
	}

	$query = new WC_Product_Query( $args );
	return $query->get_products();
}