WP_Query Multiple Category Parameters not working - php

Situation:
I cannot successfully a category parameter within WP_Query that searches within two categories mixing both OR and AND relationships.
Explanation:
I've got a search/filter that sorts by region, month, and year and it works great! When using the category_name parameter, it will successfully search for a region.
<?php // These grab data from the URL which are submitted by the form
$p_topi = $_GET['topic'];
$p_reg = $_GET['region'];
$p_mon = $_GET['mon'];
$p_year = (int) $_GET['yr'];
?>
<?php
$archive_args = array(
'post_type' => 'thought_leadership',
'category_name' => $p_reg, // Pay attention to this
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
$archive_query = new WP_Query($archive_args);
if ($archive_query->have_posts()) : while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
I was asked to add sorting by topic. I tested it by replacing the variable used with category_name and that worked well.
$archive_args = array(
'post_type' => 'thought_leadership',
'category_name' => $p_topi, // Changed the variable to topic
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
I then tried combining the two variables and it sort of worked. Specifically, it worked as an OR operator, filtering by region or topic. It will not filter by both.
$archive_args = array(
'post_type' => 'thought_leadership',
'category_name' => "$p_topi, $p_reg", // Combined the variables
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
I then tried using the + symbol to make the parameter accept both variables at the same time. It worked, but now it won't work as an OR operator. In other words, I have to select a topic and region, or the search won't find any results.
$archive_args = array(
'post_type' => 'thought_leadership',
'category_name' => "$p_topi+$p_reg", // + makes it an AND operator
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
I researched WP_Query and found this excellent reference: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
I tried category__and but it returns with no posts matching that criteria.
$archive_args = array(
'post_type' => 'thought_leadership',
'category__and' => array( 12, 5 ), // 12 = ID for topic, 5 = ID for region
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
I also tested category__in but found that it will not display children of a category. That's a problem because both topic and region have children. Like category__and it returns with no posts matching that criteria.
$archive_args = array(
'post_type' => 'thought_leadership',
'category__in' => array( 12, 5 ),
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
);
As suggested by #Muhammad tayyab, I tried using tax_query. I got it work on an && basis.
$archive_args = array(
'post_type' => 'thought_leadership',
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category', // correct
'field' => 'slug', // correct
'terms' => $p_reg,
),
array(
'taxonomy' => 'category', // correct
'field' => 'slug', // correct
'terms' => $p_topi,
)
,)
);
But I couldn't successfully get the OR operator to work with nested arrays.
$archive_args = array(
'post_type' => 'thought_leadership',
'year' => $p_year,
'monthnum'=> $p_mon,
'posts_per_page' => 100,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $p_reg,
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $p_reg,
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $p_topi,
),
),
),
);
Primary Question:
Do you know of any way to create a search which will accept 0, 1 or 2 parameters? Said differently, I need to set it up so my users can search for region and/or topic or none at all.

Your question is vague but this might help:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
You can search by multiple taxonomies this way. You can use in relation to specify and or not relationship.

Related

WP_Query tax_query multiple taxonomies and terms

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

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'
)
),
);

Wordpress tax_query "and" operator not functioning as expected

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!

Search by product color - WooCommerce

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'
),
),
);

Categories