WP_Query tax_query multiple taxonomies and terms - php

I'm having a bit of trouble returning posts with multiple taxonomies and terms. Hoping someone with far more knowledge than myself can help me understand.
I'm using a select menu to populate the select options with taxonomy information of a page (in this case, the Products page). All is good with a single taxonomy and term in the tax_query but as soon as I try to use an array to pass multiples, i'm no longer returning anything. This seems simple enough but I'm missing something. Any ideas?
Here is what I'm working with:
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'term' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'term' => $businessunit
)
)
)

You error is a key array tax_query => term should be terms
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'terms' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'terms' => $businessunit
)
)
Reference wordpress

Related

Combine two or more OR relations in WP Query? [duplicate]

I currently have a search bar on my category page which will allow the search of either the product title (custom post type) or a taxonomy of brand. I can get both of these to work independently, passing in either arguments to the WP_Query, however if I attempt to array merge them both together, neither work.
What I have is:
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$cats = array(
'taxonomy' => 'productscat',
'field' => 'id',
'key' => 'name',
'terms' => $post_category,
);
$args = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'tax_query' => $cats,
'posts_per_page' => -1,
);
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search,
),
),
'posts_per_page' => -1,
);
$merged = array_merge($args, $args2);
$search_query = new WP_Query($merged);
if ($search_query->have_posts()) :
And so on with the loop; So what am I missing with the merge arguments?
Essentially i am using 's' => $search only in one of the arrays to search the post name, which I do not need (or believe i need) in the brands taxonomy array
You can combine queries like this. You can add multiple tax_query.
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search
),
array(
'taxonomy' => 'productscat',
'field' => 'id',
'terms' => $post_category
)
)
);
$search_query = new WP_Query( $args2 );
if ( $search_query->have_posts() ) :
endif;

Combine two separate wp queries into one single query for search

I currently have a search bar on my category page which will allow the search of either the product title (custom post type) or a taxonomy of brand. I can get both of these to work independently, passing in either arguments to the WP_Query, however if I attempt to array merge them both together, neither work.
What I have is:
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$cats = array(
'taxonomy' => 'productscat',
'field' => 'id',
'key' => 'name',
'terms' => $post_category,
);
$args = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'tax_query' => $cats,
'posts_per_page' => -1,
);
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search,
),
),
'posts_per_page' => -1,
);
$merged = array_merge($args, $args2);
$search_query = new WP_Query($merged);
if ($search_query->have_posts()) :
And so on with the loop; So what am I missing with the merge arguments?
Essentially i am using 's' => $search only in one of the arrays to search the post name, which I do not need (or believe i need) in the brands taxonomy array
You can combine queries like this. You can add multiple tax_query.
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search
),
array(
'taxonomy' => 'productscat',
'field' => 'id',
'terms' => $post_category
)
)
);
$search_query = new WP_Query( $args2 );
if ( $search_query->have_posts() ) :
endif;

Add filter to wordpress query

My issue is as follows;
I have fields with no value showing on my page - I want them to be hidden, until they have a value. In my code I have a meta_value_num as a sorting option per today. But it still shows values that are 0(zero) and blanks.
Please tell me how I can filter these so that the empty ones are not shown. E.g. if I can sort everything with a value = 100 and above, that would be good enough.
This is how the code looks per today:
$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array(
'post_type' => 'clinic',
'meta_key' => $treatment,
'orderby' => 'meta_value_num',
'order' => 'DSC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'location',
'field' => 'ID',
'terms' => $tax->term_id
)
),
));
I managed to fix the filter issue.
By adding this code it now filters out everything below 20 and shows all above. Big shout out to #Cbroe for pointing me in the right direction!
'meta_value' => '20',
'meta_compare' => '>=',
Code now looks like this;
$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array(
'post_type' => 'clinic',
'meta_key' => $treatment,
'meta_value' => '20',
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'location',
'field' => 'ID',
'terms' => $tax->term_id
)
),
));

Woocommerce: Create short-code with parameters

I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.

Get all products from multiple categories (Woocommerce/Wordpress)

I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);

Categories