Custom Tax Query not be added to Relevanssi Search
-
I am using Relevannsi to search on only one custom post type but through the main WordPress search. I would like to further restrict that search query for a particular custom taxonomy value.
So I have created a function for the relevanssi_modify_wp_query filter hook where I am setting my post type and posts_per_page and the tax_query. The tax_query though is being ignored when the search returns.
My results are EVERYTHING from the restricted post type with the keyword and at my posts_per_page.
If I check the $query in relevanssi_modify_wp_query before I left the filter complete, it includes the reference to my tax_query, but when I run relevanssi_query_filter to check the final query I see no reference to this tax_query. Where can it be getting lost and why?
I did have a secondary database call (WP_Query) in my search.php but I got rid of that and that did not fix this problem.
-
Can you please show me the code you have now?
Are you returning the modified query? The
relevanssi_modify_wp_query
is a filter function and requires you to return the modified $query variable, unlikepre_get_posts
which is an action and uses a reference to the variable.function travesc_search_filter($query) { global $wpdb; if ( is_admin() || ! $query->is_main_query() ) return; if ( is_post_type_archive( 'holiday' ) || $query->is_search || is_tax() ) { $query->set( 'posts_per_page', 8 ); } if ($query->is_search) { $tax_query = array(); $meta_query = array(); $query->set('post_type','holiday'); $current_url = get_current_site(); $site_domain = $current_url->domain; $domain = explode(".", $site_domain); $domain = $domain[count($domain)-2] . "." . $domain[count($domain)-1]; $tax_query = array( array( 'taxonomy' => 'website', 'field' => 'name', 'terms' => $domain, ), ); $query->set( 'tax_query', $tax_query ); } return $query; } add_filter('relevanssi_modify_wp_query','travesc_search_filter');
The taxonomy website has values like domain.ie in it and each holiday is assigned to one of these. So in this instance it determines the value to be travelescapes.ie and it looks for holidays with the website taxonomy set to this. Before installing relevanssi, I was running this same function through pre_get_posts and it worked okay.
As I said previously the search results only show holiday post types and it is display 8 posts before pagination kicks in so these are making it to the final search query.
If I print_r($query) before return I see my tax_query reference there. But if I run this filter:
add_filter( 'relevanssi_query_filter', 'rlv_check_mysql' ); function rlv_check_mysql( $query ) { var_dump( $query ); exit(); }
The SQL shows I see the clause WHERE posts.post_type IN (‘holiday’) but I see no join to the terms table to pick up the additional tax_query. Should this be showing up here?
Thanks.
First of all, those if clauses in the beginning of your function are not necessary now that you’re not running this on
pre_get_posts
:relevanssi_modify_wp_query
only runs when Relevanssi is active and running a search.But yes, the
tax_query
should appear inrelevanssi_query_filter
in the WHERE clause.I’d debug this in Relevanssi. The tax query is first processed in the beginning of
relevanssi_do_query()
in lib/search.php, and then converted into a query restriction in the beginning ofrelevanssi_search()
. I’d see if there’s some trouble there.Were you able to find out any reason why the tax_query is not being picked up in the relevanssi_modify_wp_query filter?
Haven’t looked at this; I said “I’d debug this”, implying it’s your job =) I don’t have this particular setup on my test site in any case. As far as I can tell, there are no general problems with tax_queries in Relevanssi, so it’s something specific to your setup.
Sorry, I misread what you wrote. The thing is before I added Relevanssi to the mix, I was using this tax_query with pre_get_posts and it worked, so clearly there is some issue which how Relevanssi is handling this added tax_query, especially since it is accepting the other constrictions of post type and posts_per_page. I tried using another taxonomy and this wasn’t added to the final query either. It may have to do with my ‘setup’, as you say, and its impact on Relevanssi but I wouldn’t have a clue where to start looking for something like that.
Well, I simplified your code to the bare essentials and switched the category to something that exists on my test site, and this works:
function travesc_search_filter( $query ) { global $wpdb; $tax_query = array( array( 'taxonomy' => 'category', 'field' => 'name', 'terms' => 'cabbages', ), ); $query->set( 'tax_query', $tax_query ); return $query; } add_filter( 'relevanssi_modify_wp_query', 'travesc_search_filter' );
This sets the taxonomy correctly. Try this on your site: just hardcode one term in this in the
website
taxonomy and see if it works. If it doesn’t, something’s off on your site, as this definitely should work. If it does, then you can expand to get the dynamic term name.Thanks for responding. I have tried this as you suggest and it’s not working. To further expand, I removed the custom post type and taxonomy from the mix (thinking maybe there was something amiss in their respective registrations) and I am just using the regular posts and categories now. I’ve created two posts, each assigned to its own category. If I add a filter like you have above, regardless of which category I limit my tax_query to, it returns both posts in the search results.
If I deactivate Relevanssi and run this same tax_query through ‘pre_get_posts’ encased in
if ($query->is_search)
, the tax_query is recognised and I only get one post returned in my search results.So I have one final question. Is there anything in the Relevanssi settings which could be causing the the tax_query be ignored? I have tried both including the taxonomies in the index and not including them just to see if there’s a difference but there isn’t.
If I print out the query before I return it I see this:
WP_Query Object ( [query] => Array ( [s] => blah ) [query_vars] => Array ( [s] => blah [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => post [posts_per_page] => 8 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [search_terms_count] => 1 [search_terms] => Array ( [0] => blah ) [search_orderby_title] => Array ( [0] => P38tGnw_posts.post_title LIKE '{207169f78eab111498705e53212990982040d6a575ca0ae14293af6cc9e13542}blah{207169f78eab111498705e53212990982040d6a575ca0ae14293af6cc9e13542}' ) [order] => DESC [tax_query] => Array ( [0] => Array ( [taxonomy] => category [field] => name [terms] => News ) ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => P38tGnw_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [request] => SELECT * FROM P38tGnw_posts WHERE 1=2 [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => 1 [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => c77c7b0520bd7dd78ac4300f51479512 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => Array ( [0] => about [1] => an [2] => are [3] => as [4] => at [5] => be [6] => by [7] => com [8] => for [9] => from [10] => how [11] => in [12] => is [13] => it [14] => of [15] => on [16] => or [17] => that [18] => the [19] => this [20] => to [21] => was [22] => what [23] => when [24] => where [25] => who [26] => will [27] => with [28] => www ) [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )
So I see all the references to my tax_query set there.
But if I use the ‘relevanssi_query_filter’ filter to show the Relevanssi query before it is run, I see this:
string(576) "SELECT DISTINCT(relevanssi.doc), relevanssi.*, relevanssi.title * 5 + relevanssi.content * 5 + relevanssi.comment * 0.75 + relevanssi.tag * 0.5 + relevanssi.link * 0.75 + relevanssi.author + relevanssi.category * 0.5 + relevanssi.excerpt + relevanssi.taxonomy + relevanssi.customfield + relevanssi.mysqlcolumn AS tf FROM P38tGnw_relevanssi AS relevanssi WHERE relevanssi.term = 'blah' AND ( relevanssi.doc IN ( SELECT DISTINCT(posts.ID) FROM P38tGnw_posts AS posts WHERE posts.post_type IN ( 'post') ) ) ORDER BY tf DESC LIMIT 500"
No reference to the tax_query (only the post_type setting).
If there is nothing in the settings that could impact this then I’m not really sure where to start looking. One thing I never mentioned is that this is a Multisite installation but at present there is only one site, but I don’t think this should have any bearing.
If that basic version doesn’t work, then something is definitely wrong. Relevanssi settings shouldn’t affect this; if it were something like that, it’d be overwritten by a new tax_query parameter, if something.
I’d look to see if something else is overwriting or removing the tax_query in another filter.
You can check the tax_query Relevanssi gets with this:
add_filter( 'relevanssi_search_filters', 'rlv_check_tax_query' ); function rlv_check_tax_query( $args ) { var_dump(?$args['tax_query'] ); return $args; }
That will show the tax_query Relevanssi search processes to generate the query filter that ends up in the MySQL query.
Multisite doesn’t matter, as long as you’re searching within one site; taxonomy searching does not work across multisites.
Runing the filter ‘relevanssi_search_filters’ returns NULL so the tax_query is being removed between the ‘relevanssi_modify_wp_query’ filter where it is being set and the ‘relevanssi_search_filters’ which you say shows what gets sent to the MySQL query. I tried setting the theme to Twenty Nineteen to see if there was something in our theme files tampering with the query (but since it’s an in-house theme I am familiar with I wasn’t expecting there to be). ‘relevanssi_search_filters’ still shows NULL for the tax_query using Twenty Nineteen.
I developed the site and then added Relevanssi on top of that. I may have to go back to scratch and start over with Relevanssi there from the beginning to see if I can see anything that would be tampering with the filter but off the top of my head I don’t see anything.
And as I said when I remove Relevanssi and just use pre_get_post to alter my search query, the tax_query stays in place.
Thanks for your help.
What happens between
relevanssi_modify_wp_query
andrelevanssi_search_filters
is the taxonomy query operations inrelevanssi_do_query()
, where Relevanssi picks up the taxonomy query parts from various query variables. There you can look at what’s coming in and what’s going out –?there are many query variables that do affect the tax_query, so maybe one of those is messing up with the tax_query you want to set.Hi, I would like to know if you can check this function I did, current it works on previous version of relevanssi 3.x.x, but when I updated to the latest version its not working. I’m having the same issue from keviniseekie.
add_filter('relevanssi_modify_wp_query', 'rlv_add_meta_query'); function rlv_add_meta_query($query) { global $wpdb; $tax_query = array( 'relation' => "AND", array( 'taxonomy' => 'sectionpart', 'field' => 'slug', 'terms' => array('data'), ), array( 'taxonomy' => 'rolepart', 'field' => 'slug', 'terms' => array('member','admin','mod'), ), array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => array(2,550,1350,551,589,1353), 'operator' => 'NOT IN', ), ); $query->set('tax_query', $tax_query); return $query; }
it looks like it disregard this filter and not adding it. Can you help me in a way to fix this issue?
thanks,
- The topic ‘Custom Tax Query not be added to Relevanssi Search’ is closed to new replies.