How to Hijack WordPress Query with ‘posts_clauses’ filter for example?
-
Hello!
Right now I am doing an implementation of ElasticSearch into my WordPress/WooCommerce site. I already configured my searches and the sync of product changes to alter the ElasticSearch information. But I am having issues trying to tie the ElasticSearch response to the product query of the frontend.
What ElasticSearch does for me is, I send whatever string I want to search for, and then Elastic returns to me an array of (12) product IDs that match best with the query string (These products were previously sent and then constantly updated as the product changes). So in the end, I always end up with 12 product IDs.
Then, on the frontend I have the Woocommerce searchbar, and I want to hook/filter this search so that instead of asking the database to search for whatever was typed there, go ask my external ElasticSearch service, and then search for those 12 specific IDs on my database and display them. This should theoretically reduce the searchtime because I am giving the query the specific IDs I need, and also give me more relevant results.
I have so far tried two approaches:
1) Use the ‘pre_get_posts’ filter
I tried to use this filter by using the$query->set( 'post__in' , $a );
clause, where $a is the array of IDs I want, but I found two issues, One is that it still takes a long time because the search query is still sent through the query object, this pretty much beats the purpose of the fact that I gave the 12 IDs already to the query, it’s too slow. And also, I tried to use$query->is_main_query()
so that no other query is affected, but even with this, the products on my sidebar gets affected too!The second approach is what I feel like, sometahing closer to what I need, but I’m having one issue with it:
2) Use the ‘posts_clauses’ filter
This filter allows me to filter out each SQL Clause (where,join,groupby,orderby, and so on.) so that I pretty much leave a query that asks for 12 IDs. This works great, the only issue that I have with this is that I have no way of knowing which Query is it I’m doing at any given time. I (again) tried to use the$query->is_main_query()
, tried to use this in combination with the ‘pre_get_posts’ to add a param to the query so that I could use theglobal $wp_query;
and see what query is running at the time. But no luck, the filter works, but it affects all the query searches on the screen I am at.As I said, this hook has exactly what I need, which is parse my own SQL Query I need from the server, with the IDs that ElasticSearch gives me. But I need a way to identify that I am affecting the main query of the frontend searchbar, and only that one!
I am willing to try other approaches to hijack this query and give it 12 IDs, and I am also willing to elaborate further on the matter, since I’ve been stuck with this for a week now, and I am starting to lose hope.
Thanks to anyone who read it all and hopefully someone could chime in to help?
- The topic ‘How to Hijack WordPress Query with ‘posts_clauses’ filter for example?’ is closed to new replies.