WC_Product_Data_Store_CPT::query()publicWC 3.2.0

Query for Products matching specific criteria.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

Array|Object.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->query( $query_vars );
$query_vars(array) (required)
Query vars from a WC_Product_Query.

Changelog

Since 3.2.0 Introduced.

WC_Product_Data_Store_CPT::query() code WC 9.4.2

public function query( $query_vars ) {
	$args = $this->get_wp_query_args( $query_vars );

	if ( ! empty( $args['errors'] ) ) {
		$query = (object) array(
			'posts'         => array(),
			'found_posts'   => 0,
			'max_num_pages' => 0,
		);
	} else {
		$query = new WP_Query( $args );
	}

	if ( isset( $query_vars['return'] ) && 'objects' === $query_vars['return'] && ! empty( $query->posts ) ) {
		// Prime caches before grabbing objects.
		update_post_caches( $query->posts, array( 'product', 'product_variation' ) );
	}

	$products = ( isset( $query_vars['return'] ) && 'ids' === $query_vars['return'] ) ? $query->posts : array_filter( array_map( 'wc_get_product', $query->posts ) );

	if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) {
		return (object) array(
			'products'      => $products,
			'total'         => $query->found_posts,
			'max_num_pages' => $query->max_num_pages,
		);
	}

	return $products;
}