Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Sumit Singh

    (@5um17)

    Hi BroEmlyn,

    Sorry for the delay reply.
    Well this is not something where WP extended search has control.
    WordPress has a query var that has control over this, called as exact. by default it is false but once you make it true then you can only search by whole title e.g.

    search for mytitle = results post with title mytitle

    search for mytitle abc = results post with title mytitle abc

    search for abc = results post with title abc else nothing found

    I know this is not useful option but that is something which is very uncommon. If you still want to add this in your code, here is the code

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

    where to place the above code to work,

    in my site showing results improper, for example :
    search for rice shows results of price , can i make search for particular tag itself so that when i type ‘rice’ should show ‘rice’ tag posts itself

    immediate reply is needful , it will be grateful for fast reply

    Plugin Author Sumit Singh

    (@5um17)

    Hi Naveen,

    Place this code in your theme functions.php

    its not working sumit,

    you can check once page how it comes
    https://zoneadds.com/?s=rice

    hi sumit , it resolved by below code , which to be placed in functions.php

    // Search SQL filter for matching against post title only.
    function __search_by_title_only( $search, $wp_query )
    {
    global $wpdb;

    if ( empty( $search ) )
    return $search; // skip processing – no search term in query

    $q = $wp_query->query_vars;
    $n = ! empty( $q[‘exact’] ) ? ” : ‘%’;

    $search =
    $searchand = ”;

    foreach ( (array) $q[‘search_terms’] as $term ) {
    $term = esc_sql( like_escape( $term ) );

    $search .= “{$searchand}($wpdb->posts.post_title REGEXP ‘[[:<:]]{$term}[[:>:]]’)”;

    $searchand = ‘ AND ‘;
    }

    if ( ! empty( $search ) ) {
    $search = ” AND ({$search}) “;
    if ( ! is_user_logged_in() )
    $search .= ” AND ($wpdb->posts.post_password = ”) “;
    }

    return $search;
    }

    add_filter( ‘posts_search’, ‘__search_by_title_only’, 1000, 2 );

    This works very well for me

    //BETTER SEARCH
    add_filter( 'posts_search', 'my_search_is_perfect', 20, 2 );
    function my_search_is_perfect( $search, $wp_query ) {
        global $wpdb;
    
        if ( empty( $search ) )
            return $search;
    
        $q = $wp_query->query_vars;
        $n = !empty( $q['exact'] ) ? '' : '%';
    
        $search = $searchand = '';
    
        foreach ( (array) $q['search_terms'] as $term ) {
            $term = esc_sql( like_escape( $term ) );
    
            $search .= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]') OR ($wpdb->posts.post_content REGEXP '[[:<:]]{$term}[[:>:]]')";
    
            $searchand = ' AND ';
        }
    
        if ( ! empty( $search ) ) {
            $search = " AND ({$search}) ";
            if ( ! is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
    
        return $search;
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Want seach to match whole words only’ is closed to new replies.