0

Anybody knows if there's a hook before execution of a search query (not in main loop) in order to change the 's' parameter to avoid Wordpress' phrase splitting? Examples are welcome... thanks

P.S.: I've also tried in this way as follow, but the first part of sql query seems to indicate that WP have already done the search and then re-run a query filtered by post_ids firstly found:

function alter_the_query( $where = '' ) {
global $wpdb, $table_prefix, $wp_query;
$tmpQobj = $wp_query->queried_object;
$tmpTitle = $wp_query->queried_object->post_title;

$where .= $wpdb->prepare( " AND ({$table_prefix}posts.post_title like %s OR {$table_prefix}posts.post_content like %s )", "%{$tmpTitle}%", "%{$tmpTitle}%" );

debug($where);

return $where;
}
add_filter( 'posts_where', 'alter_the_query' );

And this is the content of request value (* stands for table prefix) returned by debug:

[request] => SELECT SQL_CALC_FOUND_ROWS  ***_posts.ID FROM ***__posts  WHERE 1=1  AND ***__posts.ID IN (534,868,911,917) AND ***__posts.post_type IN ('multimedia', 'news', 'social_project', 'publication') AND ((***__posts.post_status = 'publish')) AND (***__posts.post_title like '%string to search%' OR ***__posts.post_content like '%string to searchs%' )  ORDER BY FIELD( ***__posts.ID, 534,868,911,917 ) LIMIT 0, 3

As You can see, the original search is already done and my changes affects only the second query... I don't know why WP Core developers have done it in this way, but it's quite a nightmare...

How can I hook the first query, the one releated to original search, I mean?

2
  • What exactly do you need to do Oct 13, 2015 at 5:39
  • I need to bypass wp's phrase splitting to do an exact search in title and content. I've tried 'exact' and 'sentence' params in query args, but is not my case. I've also tried some filters but they change param but don't bypass splitting. I need to make change in sql query just a moment before real execution Oct 13, 2015 at 8:49

2 Answers 2

5

To avoid the splitting of searchphrases, use the sentence query variable in the pre_get_posts hook:

function only_search_for_full_phrase( $query ) {
    if ( $query->is_search() && $query->is_main_query() ) {
        $query->set( 'sentence', true );
    }
}
add_action( 'pre_get_posts', 'only_search_for_full_phrase' );

Didn't test it, but should do the trick.

Happy Coding!

1

You are looking for the pre_get_posts hook.

1
  • 2
    I've already tried this way, but it seems like I could only change params, not the sql query. So the phrase in 's' param, which I would do an exact match with, will pass by splitting again... It's quite a tricky case... The only alternative seems to do a direct sql query, but I also need to search in many acf fields... and this is quite a nightmare... Oct 13, 2015 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.