Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the search action in wordpress search bar?

Create a new post and publish it.

The title is my test for search, content in it is as below:

no host route

Check what happen in wordpress database.

 select post_title from wp_posts
     where post_content like "%no%"
       and post_content like "%route%"
       and post_content like "%to%"
       and post_content like "%host%";

The post named my test for search will not be in the select's result.
Type no route to host in wordpress search bar,and click enter. The post named my test for search shown as result.

enter image description here

I found the reason that the webpage contain to ,in the left upper side corner ,there is a word Customize which contains the searched word to.
How to change such search action in wordpress serach bar?
I want to make the search behavior in wordpress saerch bar, for example ,when you type no route to host, equal to the following sql command.

select post_title from wp_posts where post_content like "%no%route%to%host%";

All the plugins in my wordpress.

CodePen Embedded Pens Shortcode
Crayon Syntax Highlighter
Disable Google Fonts
Quotmarks Replacer
SyntaxHighlighter Evolved
like image 664
showkey Avatar asked Dec 21 '18 08:12

showkey


People also ask

How do I change the search function in WordPress?

How to edit the search results page. In your WordPress admin panel go to Appearance -> Editor. From the right-hand side of the Edit Themes page look for the one called Search Results (serach. php) and click on it.

How do I change the search button text in WordPress?

To change the button text, you need to go after override template to job-application. php and after that you can change the button text. Remember to backup your site before doing any change.


1 Answers

There's this addition to the SQL WHERE clause on wp-includes/class-wp-query.php:1306:

<?php
// wp-includes/class-wp-query.php:~1306

foreach ( $q['search_terms'] as $term ) {
    //...
    $like = $n . $wpdb->esc_like( $term ) . $n;
    $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
    // ...

Therefore, I'd hook into the pre_get_posts, and supply the words of the query as explicit "search_terms", since they get added into that clause, with the LIKE modifier just as you said were looking for!

So, we might do something like this:

<?php
// functions.php

function fuzzify_query(\WP_Query $q) {
    if (true === $q->is_search()
        && true === property_exists($q, 'query')
        && true === key_exists('s', $q->query)
    ) {
        $original_query = $q->query['s'];
        $words          = explode(' ', $original_query);
        $fuzzy_words    = array_map(
            function($word) {
                return '%'.$word.'%';
            },
            $words
        );

        $q->query_vars['search_terms'] = $fuzzy_words;

        return $q;
    }

    return $q;
}

 add_action('pre_get_posts', 'fuzzify_query', 100); // Or whatever priority your fuzziness requires!
like image 138
Cameron Hurd Avatar answered Nov 01 '22 09:11

Cameron Hurd