I am creating a custom filter for WooCommerce products. I have the following WP_Query to filter on selected products:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_size',
'terms' => $sizeFilter,
'field' => 'slug'
),
array(
'taxonomy' => 'product_cat',
'terms' => $catFilter,
'field' => 'slug'
),
array(
'taxonomy' => 'pa_color',
'terms' => $colorFilter,
'field' => 'slug'
)
)
);
I also want to add a meta_query to filter on product price. However, I want to find products with a given price AND the tax_query filter above. Is there a way to do a relation => 'AND' between the meta_query and tax_query?
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => $priceFilter,
'compare' => 'IN'
),
)
When I add the meta_query within the array, no products are returned. I tried separating the two queries into two arrays and then merging the arrays, but it was not a "relation => AND".
Related
I want to show product bundles as first items of a custom WooCommerce product loop.
Unfortunately I'm not sure how to order by a product type.
I know that I could order by a meta_key for example.
At the moment I'm doing this already with this lines:
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
Do I need to add a second loop to do this?
Here's are my current arguments of the loop:
$args = array(
'posts_per_page' => 14,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'post__not_in' => array (get_the_ID()),
'post_type' => array('product'),
'tax_query' => array(
array(
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'bundle',
'operator' => 'NOT IN',
),
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_stock_status',
'value' => 'instock'
),
),
);
I am trying to make a simple form to query woocommerce products wine based on form input fields as follows:
Selected category (Vine type, eg. red wine, white wine etc..) - Input type dropdown
Select Winery Tag 1 dropdown
Select Wine Sort Tag 2 dropdown
Select wine region Tag 3 dropdown
Price Range dropdown
Filtering by category and price works, however tags give mixed results and i cannot figure out why.
This is how my form looks like to give some context:
Here is my code:
$custom_query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'posts_per_page' => 3,
'product_tag' => array($tag1, $tag2, tag3),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => 'OR'
)),
//Price
'meta_query' => array(
array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);
From the input fields, I have 3 variables for product tags (like $tag1, $tag2, $tag3), 1 variable for product category (like $category) and 2 variables for the price range (like $clow and $chigh) which are the price from to.
Anyone have an idea why this happens?
There are some little mistakes in your code:
There is a missing $ in tag3,
OR is not an operator value (and it's not needed),
Each Product tag need to be in a separated tax_query array to get your filters working.
So try the following modified code instead:
$custom_query_args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'tax_query' => array(
// Product category filter
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
),
// Product tag 1 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag1),
'field' => 'slug',
),
// Product tag 2 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag2),
'field' => 'slug',
),
// Product tag 3 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag3),
'field' => 'slug',
),
),
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
Tested and works.
I have found easier way to do this and its better because when no filters are present it will display all products and the query is also smaller, like this:
$category = 'category-slug-here'; //String, but can accept array?
$tags = 'comma,delimeted,tags,here'; //I build string with tags needed no array.
$clow = 0; //min price int
$chigh = 200; //max price int
$custom_query_args = array(
'posts_per_page' => '12',
'product_cat' => $category,
'post_type' => 'product',
'product_tag' => $tags,
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
Are there any bad sides doing it like this, since i have tested it using different filter tags and it works like i planned it to?
I am working with WP_Query to pull results from DB using multiple taxonomy from a custom post type.I have two taxonomy with drop down each.Taxonomy city and cuisine but if a dont't select any one of them results don't show.Actually i want to show results only with the key words even if these taxonomies are not selected.
My codes
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
),
),
);
$restaurant_query = new WP_Query( $args );
In what way does your query know when a taxonomy term is selected? You could try to add the tax_query with an if statement later on like this:
if ($term == 'cuisine') {
$args['meta_query'][] = array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
);
} elsif($term == 'city') {
$args['meta_query'][] = array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
);
}
This why the tax_query will only be added when the term is selected. I've had a few strange encounters with this issue in the past and this method fixed my issue quite a few times.
I have a custom post type of image with a custom taxonomy called image_tag (it's hierarchical like categories). Here are some examples of the tags that might be used:
Structure (id: 25)
- House (id: 56)
- Skyscraper
Nature
- Animal
- Plant (id: 41)
So, I want to drill down through the images by selecting multiple tags in conjunction with the "and" operator. For example, finding all photos with plants and houses.
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(41, 56), // IDs of "plant" and "house"
'operator' => 'and',
),
),
);
That works fine, the problem begins when I try to include the parent terms, for example:
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'and',
),
),
);
Then I get no results. I'm guessing that because I'm using the "and" operator, Wordpress doesn't include the children of the "Structure" term. Does anyone have an idea how I can get this to work?
So it should look like this:
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 25 ),
),
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 41 ),
),
),
Update: You forget to close 'terms' and 'operator' by ' like this
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
I had a similar problem, give this a try!
I've been trying for some time now to figure out the correct $args-array for making a custom search form, allowing the user to search for products by name, description and custom WooCommerce attributes (as for now by color).
Is this possible using the WP_Query at all or do I need to alter the built in search function? And if so - how?
Here's the $args-options I've been trying for now:
$args = array(
'posts_per_page' => 20,
'no_found_rows' => true,
'post_type' => array('product'),
'tax_query' => array(
array(
'taxonomy' => 'oct-search',
'field' => 'slug',
'terms' => array($_POST["search_string"]),
),
),
);
Okay, so I solved this on my own hand, using this code:
$attributes = 'oct-shade';
$attributes = 'pa_'.$attributes;
$filters = explode(',', $_POST["search_string"]);
$args = array(
'posts_per_page' => 20,
'no_found_rows' => true,
'post_type' => array('product'),
'tax_query' =>
array(
'relation' => 'OR',
array(
'taxonomy' => "$attributes",
'field' => 'slug',
'terms' => $filters,
'operator' => 'IN'
),
),
);