Automattic\WooCommerce\Blocks\BlockTypes

ProductQuery::build_query()publicWC 1.0

Return a custom query based on attributes, filters and global WP_Query.

Метод класса: ProductQuery{}

Хуков нет.

Возвращает

Массив.

Использование

$ProductQuery = new ProductQuery();
$ProductQuery->build_query( $query );
$query(WP_Query) (обязательный)
The WordPress Query.

Код ProductQuery::build_query() WC 7.1.1

public function build_query( $query ) {
	$parsed_block = $this->parsed_block;
	if ( ! $this->is_woocommerce_variation( $parsed_block ) ) {
		return $query;
	}

	$common_query_values = array(
		'post_type'      => 'product',
		'post_status'    => 'publish',
		'posts_per_page' => $query['posts_per_page'],
		'orderby'        => $query['orderby'],
		'order'          => $query['order'],
		'offset'         => $query['offset'],
		// Ignoring the warning of not using meta queries.
		// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
		'meta_query'     => array(),
	);

	$queries_attributes = $this->get_queries_by_attributes( $parsed_block );
	$queries_filters    = $this->get_queries_by_applied_filters();

	return array_reduce(
		array_merge(
			$queries_attributes,
			$queries_filters
		),
		function( $acc, $query ) {
			if ( isset( $query['post__in'] ) ) {
				$acc['post__in'] = isset( $acc['post__in'] ) ? array_intersect( $acc['post__in'], $query['post__in'] ) : $query['post__in'];
			}
			// Ignoring the warning of not using meta queries.
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			$acc['meta_query'] = isset( $query['meta_query'] ) ? array_merge( $acc['meta_query'], array( $query['meta_query'] ) ) : $acc['meta_query'];
			return $acc;
		},
		$common_query_values
	);

}