Im having problems with wc_get_products meta query. Im trying to exclude products that have clearance
or express
in the SKU and the SKUs are formatted like so; (not my choice something the company decided many moons ago!)
My Club - Express - Item My Club - Clearance - Item Another Club - Standard - Item
Below are my args, please could someone tell me what's wrong with them?
'post_status' => array('publish', 'pending', 'draft', 'future', 'private'),
'parent' => array( 13, 14, 15 ),
'limit' => 1000,
'orderby' => 'id',
'order' => 'asc',
'type' => array( 'simple', 'variable' ),
'meta_query' => array(
'relation' => 'and',
array(
'relation' => 'or',
array(
'key' => '_sku',
'value' => 'clearance',
'compare' => 'NOT LIKE',
),
),
array(
'relation' => 'or',
array(
'key' => '_sku',
'value' => 'fxpe',
'compare' => 'NOT LIKE',
),
),
)
);
I have also completed the woocommerce steps (at bottom of page) Adding Custom Parameter Support which looks like this:
function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['_sku'] ) ) {
$query['meta_query'][] = array(
'key' => '_sku',
'value' => esc_attr( $query_vars['_sku'] ),
);
}
return $query;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
I've also tried reversing the meta query and changing the compare to like
or !=
but nothing works or seems to affect the query.
Any help really appreciate.
Thanks
UPDATE-------------------------------
I made a major typo in the initial post, to confirm I am using wc_get_products no wp_query
I have been trying a few variations of the meta query but so far nothing is working...
Just using a single meta query doesn't seem to work either
'meta_query' => array(
'key' => '_sku',
'value' => 'express',
'compare' => '!=', // also tried 'NOT LIKE'
),
Becuase of the inital confusion with the query I will mark Howard E's question correct as it is for wp_query and repost witht he correct information.
Try updating your meta_query. You're starting by saying "OR" for the relation of both clearance
and express
also.
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_sku',
'value' => 'clearance',
'compare' => 'NOT LIKE',
),
),
array(
'relation' => 'OR',
array(
'key' => '_sku',
'value' => 'express',
'compare' => 'NOT LIKE',
),
),
)
* To deal with Updated Question *
function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['_sku'] ) ) {
$query['meta_query'][] = array(
array(
'relation' => 'or',
array(
'key' => '_sku',
'value' => 'fxpe',
'compare' => 'NOT LIKE',
),
),
array(
'relation' => 'or',
array(
'key' => '_sku',
'value' => 'clearance',
'compare' => 'NOT LIKE',
),
),
);
}
return $query;
}
Then just use _sku
in the product query with a value...
$products = wc_get_products(array(
'post_status' => array('publish', 'pending', 'draft', 'future', 'private'),
'parent' => array( 13, 14, 15 ),
'limit' => 1000,
'orderby' => 'id',
'order' => 'asc',
'type' => array( 'simple', 'variable' ),
'_sku' => true,
));
adding this filter will however filter any instance of _sku
in the WC query. You may want to add additional conditionals if needed.