Issue with Woocommerce query by meta and attributes - php

I have 3 products having meta key 'check_key' = 1. 2 of them have attribute 'pa_size_10_20' with values '100' and '30'; other one have attribute 'pa_size_30_40' with value '70'.
Please help me understand why the query below returns all 3 products? I expect only 2 products with 'pa_size_10_20' attribute.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => 0,
'operator' => '!='
)
)
);
$posts = get_posts( $args );
TIA

It seems as though your tax_query would return all taxonomy terms that aren't named 0 an integer?
I think if you know the values for IN then I would use those instead.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
),
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => array('100', '300'),
'operator' => 'IN'
),
),
);

Related

How to filter WP_Query with 'orderby' (ASC & DESC) with meta_query?

I'm trying to filter posts by 'DESC' with custom meta: 'like_count_on_post', then collecting all empty 'like_count_on_post' with 'dislike_count_on_post', and then finishing with 'ASC' of 'dislike_count_on_post', but I only get likes descending, or if I remove:
'custom_field_value' => 'DESC'
I can get ascending of dislikes, but not both.
The query arguments code:
$args = array(
'post_status' => 'publish',
'post_type' => 'sveikinimai',
'meta_query' => array(
"relation" => "and",
'likes' => array(
"relation" => "or",
'custom_field_value' => array(
'key' => '_like_count_on_post_',
),
'custom_field' => array(
'key' => '_like_count_on_post_',
'compare' => 'NOT EXISTS',
),
),
'dislikes' => array(
"relation" => "or",
'custom_field_value_2' => array(
'key' => '_dislike_count_on_post_',
),
'custom_field_2' => array(
'key' => '_dislike_count_on_post_',
'compare' => 'NOT EXISTS',
),
),
),
'orderby' => array(
'custom_field_value' => 'DESC',
'custom_field_value_2' => 'ASC'
),
'posts_per_page' => 20,
'paged' => $paged,
);
Update, here is code if you want to filter without non existing meta fields:
$args = array(
'post_status' => 'publish',
'post_type' => 'sveikinimai',
'meta_query' => array(
"relation" => "and",
'custom_field_value' => array(
'key' => '_like_count_on_post_',
),
'custom_field_value_2' => array(
'key' => '_dislike_count_on_post_',
),
),
'orderby' => array(
'custom_field_value' => 'DESC',
'custom_field_value_2' => 'ASC'
),
'posts_per_page' => 20,
'paged' => $paged,
);
To solve problem, I added to all posts 'like_count_on_post' and 'dislike_count_on_post' meta fields. Filter working as it should, I suppose doesn't resolve when fields empty, but here is code to make those fields not empty:
add_action('save_post', 'add_post_custom_meta');
function add_post_custom_meta() {
global $post;
$post_id = $post->ID;
update_post_meta($post_id, '_like_count_on_post_', 0);
update_post_meta($post_id, '_dislike_count_on_post_', 0);
}

How do I use a tax_query and a meta_query in wordpress succesfully?

This is the code I am using:
$results = array(
'post_type' => 'score',
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'numberposts' => -1,
'post_type' => 'score',
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query($results);
The problem is I can't work out how to make a Boolean AND expression so that both the taxonomy and the custom field should match before returning a post.
I revised your code. check below code.
$results = array(
'post_type' => 'score',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $results );

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

WP_Query not ordered correctly

I have a custom post type cota where all data is displayed in a <table> in my site. I need to order this table by:
administradora - some kind of "category" that the user specifies. Ordered ASC (a-z);
valor - The price of the cota. Each cota has a price. Ordered DESC (9 - 0) ;
The problem : It's already ordered by administradora, but not correctly ordered by valor.
Example: The order by valor displays first a cota with US$9.000,00 and then another cota with US$1.000.000,00. It's wrong. The one million dollar cota should come first, and then the nine thousand dollars cota.
Here's an image that shows better the situation
Code for ordering the cota:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
EDIT 1: After some research, i found out that wordpress saves all its data as longtext in the database. But still cannot solve the problem.
I can provide more info if needed.
Try Below Code:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'id',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
SOLVED
The following code should work:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'meta_query' => array(
'relation' => 'AND',
'credito_clause' => array(
'key' => "valor",
'orderby' => 'meta_value_num',
'type' => 'DECIMAL',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'credito_clause' => 'DESC'
)
)
);

Wordpress WP_Query with multiple meta_values for a meta_key

I'm trying to run a WP_Query in order to search for all the products in my database with multiple meta values.
e.g
Product 1 -> meta_key['key1'] ->meta_value['value1']
Product 2 -> meta_key['key1'] ->meta_value['value2']
Product 3 -> meta_key['key1'] ->meta_value['value3']
So i want to get all three products.My arguments are
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key1',
'value' => 'val1',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val2',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val3',
'compare' => '='
),
),
'paged' => $paged
);
The problem is that no products are returned. Instead , if a give only one meta_key => meta_value pair it works fine
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'key1',
'meta_value' =>'val1',
'paged' => $paged
);
You should use IN as compare value, like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key1',
'value' => array('val1','val2','val3'),
'compare' => 'IN',
),
),
'paged' => $paged
);
Hope it helps!

Categories