A Client’s Unique Challenge
One of our clients was using the Filter Everything Pro plugin along with Elementor to filter post articles. Everything was working well, but they had a special request that the plugin didn’t support out of the box.
Their requirements were simple yet specific:
- The search should only look in post titles and excerpts, completely ignoring the post content.
- The search should match the exact word, meaning that searching for “inter” should not return results for “interview.”
At first glance, this seemed like an easy fix, but things got complicated when we dived into how Filter Everything Pro handled searches.
The Problem with Default Search
Filter Everything Pro, like most WordPress search plugins, relies on the default WordPress search algorithm. It uses its own hooks to modify the query:
- wpc_filtered_query_end
- wpc_all_set_wp_queried_posts
The problem? WordPress’s default search uses the LIKE operator, which means it searches for partial matches across post content as well as titles. If you searched for “inter,” the results would include “interview,” “international,” and anything else containing “inter.”
The client didn’t want this behavior. They wanted exact word matching in only the post title and excerpt.
Additionally, they found that when users searched for a term, irrelevant results were displayed due to how WordPress ranks search matches. This was causing frustration as users had to sift through numerous posts that weren’t truly relevant to their searches.
Unfortunately, Filter Everything Pro did not offer a built-in option to modify this, so we had to get creative with custom code.
The Solution: Custom Code Modification
We needed to override the default behavior of Filter Everything Pro and WordPress search by writing our own custom functions.
Step 1: Remove Default Hooks
First, we had to remove the plugin’s default search behavior to prevent interference:
add_action('wp_loaded', function() { remove_action('wpc_filtered_query_end', [ $this, 'addSearchArgsToWpQuery' ]); remove_action('wpc_all_set_wp_queried_posts', [ $this, 'addSearchArgsToWpQuery' ]); // Re-register our custom function add_action( 'wpc_filtered_query_end', 'forceSearchToABC' ); add_action( 'wpc_all_set_wp_queried_posts', 'forceSearchToABC' ); });
Step 2: Modify the Search QueryataÂ
Unlike WordPress’s default search, Filter Everything Pro uses srch instead of s as the GET parameter for searches. Knowing this, we were able to modify the search query to only look in post titles and excerpts:
function forceSearchToABC( $wp_query ) { if ( isset($_GET['srch']) ) { $search_term = $_GET['srch']; $wp_query->set( 's', $search_term ); $wp_query->set('exact', true); // Ensure exact match search $wp_query->set( 'fields', 'ids' ); // Optimize query by fetching only post IDs $wp_query->set( 'search_columns', array( 'post_title', 'post_excerpt' ) ); // Limit search to title and excerpt } return $wp_query; }
At this point, our search was now ignoring post content and only searching in the title and excerpt. But we still had one problem left: WordPress was still using LIKE, which meant it was matching partial words.
Step 3: Enforcing Exact Word Matching with REGEXP
To fix this, we needed to **replace LIKE with **REGEXP so that WordPress would search for full words only. We accomplished this with another function:
function exact_search_filter($search, $wp_query) { if (!empty($wp_query->query_vars['exact'])) { // Wrap search term with word boundaries to enforce exact matches $search_term = '\b' . preg_quote($wp_query->query_vars['s'], '/') . '\b'; // Modify the search query to use REGEXP instead of LIKE $search = str_replace('LIKE', 'REGEXP', $search); $search = str_replace('%', '', $search); $search = str_replace($wp_query->query_vars['s'], $search_term, $search); } return $search; } add_filter('posts_search', 'exact_search_filter', 10, 2);
By using REGEXP and the \b word boundary markers, we ensured that searches would now only return exact word matches.
Additionally, we made sure that our function was optimized to avoid unnecessary database queries, improving overall search speed and performance.
Additional Benefits of the Modification
This modification did more than just improve search accuracy; it also enhanced the user experience in several ways:
- Faster Search Results:Since we limited the search to titles and excerpts and retrieved only post IDs, the database queries became much more efficient.
- Improved Search Relevance:By enforcing exact word matching, users no longer had to sift through irrelevant results.
- Seamless Integration:The solution works within the existing Filter Everything Pro structure without breaking other functionalities.
- Better SEO Optimization:More accurate search results mean better user engagement and retention on the site.
The client was thrilled with the improvement! Now, when they searched for “inter,” it no longer returned results for “interview”—only posts that had “inter” as a standalone word in the title or excerpt appeared.
This modification to Filter Everything Pro showcases how powerful custom WordPress hooks can be. If you ever find yourself limited by a plugin’s default search behavior, you can extend it with your own code to make it work exactly the way you need.
If you’re using Filter Everything Pro and want better search accuracy, give this solution a try!
For more information about the Filter Everything Pro plugin, visit Filter Everything Pro Plugin.
0 Comments