WC_Data_Store_WP::get_wp_query_args()protectedWC 3.1.0

Get valid WP_Query args from a WC_Object_Query's query variables.

Method of the class: WC_Data_Store_WP{}

Hooks from the method

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_wp_query_args( $query_vars );
$query_vars(array) (required)
query vars from a WC_Object_Query.

Changelog

Since 3.1.0 Introduced.

WC_Data_Store_WP::get_wp_query_args() code WC 9.4.2

protected function get_wp_query_args( $query_vars ) {

	$skipped_values = array( '', array(), null );
	$wp_query_args  = array(
		'errors'     => array(),
		'meta_query' => array(), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	);

	foreach ( $query_vars as $key => $value ) {
		if ( in_array( $value, $skipped_values, true ) || 'meta_query' === $key ) {
			continue;
		}

		// Build meta queries out of vars that are stored in internal meta keys.
		if ( in_array( '_' . $key, $this->internal_meta_keys, true ) ) {
			// Check for existing values if wildcard is used.
			if ( '*' === $value ) {
				$wp_query_args['meta_query'][] = array(
					array(
						'key'     => '_' . $key,
						'compare' => 'EXISTS',
					),
					array(
						'key'     => '_' . $key,
						'value'   => '',
						'compare' => '!=',
					),
				);
			} else {
				$wp_query_args['meta_query'][] = array(
					'key'     => '_' . $key,
					'value'   => $value,
					'compare' => is_array( $value ) ? 'IN' : '=',
				);
			}
		} else { // Other vars get mapped to wp_query args or just left alone.
			$key_mapping = array(
				'parent'         => 'post_parent',
				'parent_exclude' => 'post_parent__not_in',
				'exclude'        => 'post__not_in',
				'limit'          => 'posts_per_page',
				'type'           => 'post_type',
				'return'         => 'fields',
			);

			if ( isset( $key_mapping[ $key ] ) ) {
				$wp_query_args[ $key_mapping[ $key ] ] = $value;
			} else {
				$wp_query_args[ $key ] = $value;
			}
		}
	}

	return apply_filters( 'woocommerce_get_wp_query_args', $wp_query_args, $query_vars );
}