WC_Product_Data_Store_CPT::get_wp_query_args( array $query_vars )
Get valid WP_Query args from a WC_Product_Query’s query variables.
Description Description
Parameters Parameters
- $query_vars
-
(Required) Query vars from a WC_Product_Query.
Return Return
(array)
Source Source
File: includes/data-stores/class-wc-product-data-store-cpt.php
protected function get_wp_query_args( $query_vars ) {
// Map query vars to ones that get_wp_query_args or WP_Query recognize.
$key_mapping = array(
'status' => 'post_status',
'page' => 'paged',
'include' => 'post__in',
'stock_quantity' => 'stock',
'average_rating' => 'wc_average_rating',
'review_count' => 'wc_review_count',
);
foreach ( $key_mapping as $query_key => $db_key ) {
if ( isset( $query_vars[ $query_key ] ) ) {
$query_vars[ $db_key ] = $query_vars[ $query_key ];
unset( $query_vars[ $query_key ] );
}
}
// Map boolean queries that are stored as 'yes'/'no' in the DB to 'yes' or 'no'.
$boolean_queries = array(
'virtual',
'downloadable',
'sold_individually',
'manage_stock',
);
foreach ( $boolean_queries as $boolean_query ) {
if ( isset( $query_vars[ $boolean_query ] ) && '' !== $query_vars[ $boolean_query ] ) {
$query_vars[ $boolean_query ] = $query_vars[ $boolean_query ] ? 'yes' : 'no';
}
}
// These queries cannot be auto-generated so we have to remove them and build them manually.
$manual_queries = array(
'sku' => '',
'featured' => '',
'visibility' => '',
);
foreach ( $manual_queries as $key => $manual_query ) {
if ( isset( $query_vars[ $key ] ) ) {
$manual_queries[ $key ] = $query_vars[ $key ];
unset( $query_vars[ $key ] );
}
}
$wp_query_args = parent::get_wp_query_args( $query_vars );
if ( ! isset( $wp_query_args['date_query'] ) ) {
$wp_query_args['date_query'] = array();
}
if ( ! isset( $wp_query_args['meta_query'] ) ) {
$wp_query_args['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
// Handle product types.
if ( 'variation' === $query_vars['type'] ) {
$wp_query_args['post_type'] = 'product_variation';
} elseif ( is_array( $query_vars['type'] ) && in_array( 'variation', $query_vars['type'], true ) ) {
$wp_query_args['post_type'] = array( 'product_variation', 'product' );
$wp_query_args['tax_query'][] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'relation' => 'OR',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $query_vars['type'],
),
array(
'taxonomy' => 'product_type',
'field' => 'id',
'operator' => 'NOT EXISTS',
),
);
} else {
$wp_query_args['post_type'] = 'product';
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $query_vars['type'],
);
}
// Handle product categories.
if ( ! empty( $query_vars['category'] ) ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $query_vars['category'],
);
}
// Handle product tags.
if ( ! empty( $query_vars['tag'] ) ) {
unset( $wp_query_args['tag'] );
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $query_vars['tag'],
);
}
// Handle shipping classes.
if ( ! empty( $query_vars['shipping_class'] ) ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_shipping_class',
'field' => 'slug',
'terms' => $query_vars['shipping_class'],
);
}
// Handle total_sales.
// This query doesn't get auto-generated since the meta key doesn't have the underscore prefix.
if ( isset( $query_vars['total_sales'] ) && '' !== $query_vars['total_sales'] ) {
$wp_query_args['meta_query'][] = array(
'key' => 'total_sales',
'value' => absint( $query_vars['total_sales'] ),
'compare' => '=',
);
}
// Handle SKU.
if ( $manual_queries['sku'] ) {
// Check for existing values if wildcard is used.
if ( '*' === $manual_queries['sku'] ) {
$wp_query_args['meta_query'][] = array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
array(
'key' => '_sku',
'value' => '',
'compare' => '!=',
),
);
} else {
$wp_query_args['meta_query'][] = array(
'key' => '_sku',
'value' => $manual_queries['sku'],
'compare' => 'LIKE',
);
}
}
// Handle featured.
if ( '' !== $manual_queries['featured'] ) {
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
if ( $manual_queries['featured'] ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['featured'] ),
);
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['exclude-from-catalog'] ),
'operator' => 'NOT IN',
);
} else {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['featured'] ),
'operator' => 'NOT IN',
);
}
}
// Handle visibility.
if ( $manual_queries['visibility'] ) {
switch ( $manual_queries['visibility'] ) {
case 'search':
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array( 'exclude-from-search' ),
'operator' => 'NOT IN',
);
break;
case 'catalog':
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array( 'exclude-from-catalog' ),
'operator' => 'NOT IN',
);
break;
case 'visible':
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
'operator' => 'NOT IN',
);
break;
case 'hidden':
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
'operator' => 'AND',
);
break;
}
}
// Handle date queries.
$date_queries = array(
'date_created' => 'post_date',
'date_modified' => 'post_modified',
'date_on_sale_from' => '_sale_price_dates_from',
'date_on_sale_to' => '_sale_price_dates_to',
);
foreach ( $date_queries as $query_var_key => $db_key ) {
if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] ) {
// Remove any existing meta queries for the same keys to prevent conflicts.
$existing_queries = wp_list_pluck( $wp_query_args['meta_query'], 'key', true );
foreach ( $existing_queries as $query_index => $query_contents ) {
unset( $wp_query_args['meta_query'][ $query_index ] );
}
$wp_query_args = $this->parse_date_for_wp_query( $query_vars[ $query_var_key ], $db_key, $wp_query_args );
}
}
// Handle paginate.
if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) {
$wp_query_args['no_found_rows'] = true;
}
// Handle reviews_allowed.
if ( isset( $query_vars['reviews_allowed'] ) && is_bool( $query_vars['reviews_allowed'] ) ) {
add_filter( 'posts_where', array( $this, 'reviews_allowed_query_where' ), 10, 2 );
}
// Handle orderby.
if ( isset( $query_vars['orderby'] ) && 'include' === $query_vars['orderby'] ) {
$wp_query_args['orderby'] = 'post__in';
}
return apply_filters( 'woocommerce_product_data_store_cpt_get_products_query', $wp_query_args, $query_vars, $this );
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |