WC_Order_Data_Store_CPT::query()publicWC 3.1.0

Query for Orders matching specific criteria.

Method of the class: WC_Order_Data_Store_CPT{}

Return

Array|Object.

Usage

$WC_Order_Data_Store_CPT = new WC_Order_Data_Store_CPT();
$WC_Order_Data_Store_CPT->query( $query_vars );
$query_vars(array) (required)
query vars from a WC_Order_Query.

Changelog

Since 3.1.0 Introduced.

WC_Order_Data_Store_CPT::query() code WC 9.4.2

public function query( $query_vars ) {
	/**
	 * Allows 3rd parties to filter query args that will trigger an unsupported notice.
	 *
	 * @since 9.2.0
	 *
	 * @param array $unsupported_args Array of query arg names.
	 */
	$unsupported_args = (array) apply_filters(
		'woocommerce_order_data_store_cpt_query_unsupported_args',
		array( 'meta_query', 'field_query' )
	);

	// Trigger doing_it_wrong() for query vars only supported in HPOS.
	$unsupported_args_in_query = array_keys( array_filter( array_intersect_key( $query_vars, array_flip( $unsupported_args ) ) ) );

	if ( $unsupported_args_in_query && __CLASS__ === get_class( $this ) ) {
		wc_doing_it_wrong(
			__METHOD__,
			esc_html(
				sprintf(
					// translators: %s is a comma separated list of query arguments.
					_n(
						'Order query argument (%s) is not supported on the current order datastore.',
						'Order query arguments (%s) are not supported on the current order datastore.',
						count( $unsupported_args_in_query ),
						'woocommerce'
					),
					implode( ', ', $unsupported_args_in_query )
				)
			),
			'9.2.0'
		);
	}

	$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'] ) && 'ids' === $query_vars['return'] ) {
		$orders = $query->posts;
	} else {
		update_post_caches( $query->posts ); // We already fetching posts, might as well hydrate some caches.
		$order_ids = wp_list_pluck( $query->posts, 'ID' );
		$orders    = $this->compile_orders( $order_ids, $query_vars, $query );
	}

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

	return $orders;
}