we are writing a custom search function which searches for the posts by given Term OR Post Meta OR post_title
We have multiple terms and Post Meta values and We're looking for the solution to apply OR operation on few meta & term values.
bellow is the query that we have so far but ofcourse, is not working as we required
$search_keyword = $_GET['search_keyword'];
$args = array(
'post_type' => 'product',
'post_title' => $search_keyword,
'tax_query' => array(
array(
'taxonomy' => 'property-city',
'terms' => $search_keyword,
'field' => 'slug',
'operator' => 'LIKE'
),
array(
'taxonomy' => 'property-type',
'terms' => array('sale', 'rent'),
'field' => 'slug',
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => 'min_beds',
'value' => '2',
'compare' => '>=',
),
array(
'key' => 'min_baths',
'value' => '1',
'compare' => '>=',
),
array(
'key' => 'address',
'value' => $search_keyword,
'compare' => 'LIKE',
)
)
);
$the_query = new WP_Query($args);
$search_keyword contains the string value that needs to search. And only those posts should be selected that have a meta key matched with given $search_keyword OR matched with term OR matched with Post Title
also, keeping the other conditions as it is.
NOTE: relation b/w Taxonomy property-type and Post Meta min_beds/min_baths should be AND
I'm pretty sure this is not possible. You can either write custom SQL, or just do two or three queries and merge the results - that would probably be far easiest.
Related
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".
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.
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 );
I currently have a checkbox based search and filter on my wordpress site.
Basically it works using this wp_query
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>999999,
'category__and' => $_POST['myListOfCategories']));
I want to move away from using categories (as it's making using the blog a pain)
So I've set everything up using custom taxonomies now and notice there is no "taxonomy__and".
Does anyone know of a way to use WP_Query to search for posts using taxonomies in the same way category__and and works?
e.g. I pass multiple taxonomy id's and it only returns posts which have all of them linked.
From http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
You should be able to use something like this;
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' )
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
You have pass multiple taxonomy in Query_posts in Wordpress
See below URL It is very help full to you:-
https://wordpress.stackexchange.com/questions/25999/how-to-pass-url-parameters-for-advanced-taxonomy-queries-with-multiple-terms-for
https://wordpress.stackexchange.com/questions/10713/wp-3-1-getting-tax-query-to-work-in-query-posts
Query multiple custom taxonomy terms in Wordpress 2.8?
or try it
query_posts( array(
'tax_query' => array(
array(
'taxonomy' => 'tax1',
'field' => 'slug',
'terms' => array('term1', 'term2'),
'operator' => 'OR'
),
array(
'taxonomy' => 'tax2',
'field' => 'slug',
'terms' => array('term3', 'term4'),
'operator' => 'AND'
),
) );
With the help of the other posters here is the final answer to my question:
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>10,'tax_query' => array(
array(
'taxonomy' => 'toy_cats',
'field' => 'id',
'terms' => array(14,20,39,42),
'operator' => 'AND' )
)));
The above code will only show posts that have the "toy" post type, is in the taxonomy "toy_cats" and is is assigned to ALL the following term id's 14 AND 20 AND 39 AND 42
Hope this helps someone else.
I'm using WP_Query (pretty standard). It all works great.
However, I have a particular modification to make, where, if the user enters the specific post name in the URL, the search will return only the post that matches that post_name value.
See my code below with a comment about the particular line not working.
<?php
$getPeople = array(
'post_type' => 'person',
'posts_per_page' => -1,
// I want this below to only return me the post with this specific value.
// This doesn't error, but doesn't work either.
// I know it seems counter-productive to a 'search' but this particular case requires it.
// This has a hard-coded value at the moment.
'post_name' => 'rebecca-atkinson',
'orderby' => 'meta_value',
'meta_key' => 'last-name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'gender',
'value' => $theGender,
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'accent',
'field' => 'slug',
'terms' => $theAccent,
'operator' => 'IN',
),
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $theStyle,
'operator' => 'IN',
),
array(
'taxonomy' => 'age',
'field' => 'slug',
'terms' => $theAge,
'operator' => 'IN',
),
)
);
$myposts = new WP_Query($getPeople);
?>
Your help would be greatly appreciated. If I could just see how to search on this specific 'slug' then that would be great.
Many thanks,
Michael.
Instead of
'post_name' => 'rebecca-atkinson',
use:
'name' => 'rebecca-atkinson',
In addition to my answer in the comments above, I thought I'd post it as an official answer too:
I have to use 'name' and NOT 'post_name'.
For example:
'name' => 'rebecca-atkinson'
Hope this helps someone in future.