Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::get_results()publicWC 1.0

Get results of query.

Method of the class: ProductQuery{}

No Hooks.

Return

Array.

Usage

$ProductQuery = new ProductQuery();
$ProductQuery->get_results( $request );
$request(\WP_REST_Request) (required)
Request data.

ProductQuery::get_results() code WC 8.7.0

public function get_results( $request ) {
	$query_args = $this->prepare_objects_query( $request );

	add_filter( 'posts_clauses', [ $this, 'add_query_clauses' ], 10, 2 );

	$query       = new \WP_Query();
	$results     = $query->query( $query_args );
	$total_posts = $query->found_posts;

	// Out-of-bounds, run the query again without LIMIT for total count.
	if ( $total_posts < 1 && $query_args['paged'] > 1 ) {
		unset( $query_args['paged'] );
		$count_query = new \WP_Query();
		$count_query->query( $query_args );
		$total_posts = $count_query->found_posts;
	}

	remove_filter( 'posts_clauses', [ $this, 'add_query_clauses' ], 10 );

	return [
		'results' => $results,
		'total'   => (int) $total_posts,
		'pages'   => $query->query_vars['posts_per_page'] > 0 ? (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ) : 1,
	];
}