WP_Query get the results weather selected or not - php

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.

Related

Wordpress Query relation AND with meta_query and tax_query

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".

WP_Query not working when using tax_query

I'm trying to do a WP_Query in a custom page template to retrieve posts that are in the category with id 4003 AND either in category with ID $category_id OR have the tag with slug $category_slug.
I'm using this code:
<?php $args3 = array(
'post_type' => 'post',
'posts_per_page' => 3,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4003,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'tag',
'field' => 'slug',
'terms' => $category_slug
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $category_id
)
)
),
);
$query3 = new WP_Query( $args3 );
?>
It seems that the problem is the tag query, because posts with both categories 4003 and $category_id are correctly retrieved. I know that there are posts with category 4003 and tag $category_slug but I cannot retrieve them.
What am I doing wrong?

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?

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!

Query posts from two post formats within a category

I have been reading the WP_Query Codex looking for a way to loop through all the posts that have the post format 'video' OR 'image', within a given category.
If this wasn't enough, this category is given by a variable $catslug (I need it to be this way).
I have only found ways of looping through one of the following
image OR video
image AND category
video AND category,
but what I need is more complex, something like this:
post-format-image AND $catslug) OR (post-format-video AND $catslug)
Is it possible to do a tax_query within a tax_query?
Something like this:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' )
)
)
)
);
$query = new WP_Query( $args );
Anyone knows any workaround or a hack to get this?
Perhaps I'm just thinking the wrong way.
This actually a good question. The simple answer here is that you cannot use multiple tax_query's.
This had me quickly testing the following scenario before I left for work. Just for fun I went and tried to make use of the category parameters with a tax_query, but that gave me posts from the desired category and posts that also belongs to both post formats
I came up with a possible solution, unfortunately I cannot test this right now.
Here is my line of though here:
As you need random results, I would suggest that you add your post formats into an array
$post_format_array = array( 'post-format-video', 'post-format-image' );
You now are going to use shuffle to shuffle the array, and then take the first entry from that array, which will be either video of image, and use that in your tax_query.
shuffle( $post_format_array );
This way you will get posts that is in your desired category and in either video or image post format.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $catslug,
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_format_array[0]
),
),
);
$query = new WP_Query( $args );

Categories