Wordpress tax_query "and" operator not functioning as expected - php

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!

Related

Custom related posts with product attributes

I try to get custom related posts for my woocommerce product page based on its attributes.
I set up the following php query, to find the products with the exact same attributes:
$pa_farbe = $attributes["pa_farbe"];
$pa_material = $attributes["pa_material"];
$pa_steine = $attributes["pa_steine"];
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $parent_cat,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
'value' => array($price_min, $price_max),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_material',
'field' => 'slug',
'terms' => $pa_material->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_steine',
'field' => 'slug',
'terms' => $pa_steine->get_slugs(),
'operator' => 'IN',
)
),
'exclude' => array( $product_id )
));
However, not all products have an attribute "pa_farbe" assigned. So for those products the php code shows an error on the frontend.
How can I check before, if the attribute is available for the current product?
I tried getting the attributes with this code:
foreach( $product->get_attributes() as $attr_name => $attr ){
echo wc_attribute_taxonomy_name( $attr_name );
}
but I don't know how to use an if query in the get_posts function.
I'm not an experienced developer so in my head this would be a solution that works but is not allowed in get_posts:
if (!sizeof($pa_farbe)=0) {
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
}
Ideally the 'tax_query' => array() is filled with the current products attributes, could someone help me out here?

How can i check if the category has products or empty wordpress?

hello guys i'm trying to know if a specific category with some custom attributes has a product or not but i don't know how to use this function to check if it has or no
$isset_products_in_category = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'relation'=>'AND',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '5',
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'operator' => 'IN',
'terms' => '2',
),
),
'ignore_stickie_posts' => true,
'fields' => 'ids',
)
);
'relation' => 'AND' should be part of the tax_query array in WP_Query() (it defines the relationship between the arrays in the tax_query).
Your not doing anything with the result of the query. You could check the count:
Revised code example:
<?php
$wp_query = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '5',
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'terms' => '2',
'operator' => 'IN',
),
),
'ignore_sticky_posts' => true,
'fields' => 'ids',
)
);
// Do something with the result of the query
$isset_products_in_category = $wp_query->post_count > 0;
var_dump($isset_products_in_category); // will output bool(true) or bool(false).

WP_Query Multiple Category Parameters not working

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.

orderby and order filter in get_posts or WP_query function in wordpress not working

I have function in wordpress plugin which queries the posts using get_posts($array). But I wanted this to orderby post_modified field of the posts table in DESCENDING order, for which I have this code below:
$arrPostDtls = get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft') ,
'orderby' => 'post_modified',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Here, I did implemented the orderby or order clauses to get it sorted accordingly, but it doesnt work. Please suggest or help me to get the sorting as I am willing to.
UPDATE To get the things other way, I used the WP_query method to get the posts. for which I implemented below code:
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => 'modified',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
From this I recieved the result which also contains the SQL query, and executing the SQL query in PHPmyadmin, I found the exepected result, but when i iterated the "$arrPostDtls->posts", it still gives me the old results.. Please suggest what is wrong here..
Try
get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending',
'trash','draft','auto-draft') ,
'orderby' => array('post_modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Update after you update the question
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => array('modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
I found my problem on the UI side (thanks to #Zhilevan) where by ajax response was being exposed to a jQuery library DataTable(), whose default ordering was sorting in alphabetical order. I set the ordering parameter to false as:
$("#someid").DataTable({"ordering":false});
And My results were displayed as I was willing to

WordPress is it possible to have search by keyword of standard search query and custom tax_query

have query like this:
$key = 'xbox';
$test = get_posts(
array(
'suppress_filters' => false,
's' => $key,
'numberposts' => 10,
'post_type' => array('games'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'platforms',
'field' => 'slug',
'terms' => $key,
),
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => $key,
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $key,
)
)
)
);
and I need to have search s and tax-query working by both..
for example if I seach xbox I want that query would find and in my posts content/titlte word xbox.. but also check if any post exists in term LIKE %xbox%
any suggestions?

Categories