I have following QUERY:
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array(17, 21, 18),
'operator' => 'AND'
)
)
);
When I put 2 arguments in the array it is working but when I put 3 arguments in the array it is not working.
I can't find the answer anywhere, can someone help me please?
Related
I am creating a simple Query that should query through a meta value containing a number. 1 should be first, 2 should be 2nd and 3 should be 3rd.
For some reason, it comes out like 1, 3, 2 in my query. What am I missing??
$args = array(
'post_type' => 'x-portfolio',
'posts_per_page' => $count,
'paged' => $paged,
'orderBy' => 'meta_value_num',
'meta_key' => 'liste_nr',
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'term_id',
'terms' => $filters,
),
array(
'taxonomy' => 'portfolio-category',
'field' => 'name',
'terms' => 'Accessories',
'operator' => 'NOT IN'
)
)
);
I've run into this before when a CPT, plugin etc has done a custom query and they never reset and it can completely override yours.
Try dropping wp_reset_query() before it and make sure you are using it after.
https://developer.wordpress.org/reference/functions/wp_reset_query/
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.
I am using this query to show certain posts.
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
APP_TAX_STORE => $term->slug,
),
) );
Now instead of showing all posts from APP_TAX_STORE => $term->slug I would like to exclude them. I tried all solutions found in this form, but nothing worked. Any Ideas?
You can use tax_query.
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
) );
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.
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'
)
),
);