One possibility would be to try and alter the search SQL query. There’s a filter for that.
$search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
and (sidenote) a filter to influence the ORDER BY
clause as well
$search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
I haven’t taken a look at search for quite some time, but when I look at WP_Query::parse_search
, I could find something that might be of interest and worth giving a try.
-
Look at
protected function parse_search( &$q ) {
which has the full stack of Query variables in it, so everything added actually lands inside that function. And in there is the following line:
$n = ! empty( $q['exact'] ) ? '' : '%';
If
$q['exact']
is emtpy, then$n
will be empty as well. This is what happens next:if ( $n ) $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
and
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
So if you have a query var(?) named
exact
which needs to beTRUE
, you should be able to just run exact matches instead of the defaultLIKE
clause followed by every match that is in the middle of anything. -
And as it’s
protected
, it may get overridden by anextend
ing class. So you could just go and createclass My_Search_Query extends WP_Query
and define your own internals to generate the needed SQL statement.