Site logo

[Support request] How to search only by Post Title?

Home Forums Support [Support request] How to search only by Post Title?

Home Forums Support How to search only by Post Title?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2030738
    Miguel

    I see that by default, when I write something in the “Search Box”, the search process looks through the “Post Titles” and also the “Post Content” in order to show the search results.
    Is there a way to get the search results based only on the “Post Titles” ??

    Thanks in Advance,
    Miguel

    #2030789
    Elvin
    Staff
    Customer Support

    Hi Miguel,

    Will this search function have to be toggle-able or is it fine if the search function only searches based on Post titles?

    If it has to be toggle-able then it’s best to get a search plugin that can do this for you and replace the default plugin with this PHP snippet.

    add_filter( 'generate_navigation_search_output', function() {
        return sprintf(
            '<div class="navigation-search">%s</div>',
            do_shortcode( '[your-search-plugin-shortcode]' )
        );
    } );

    If you only want to change the search query to search through post titles only then try the snippet shared in this article – https://www.isitwp.com/limit-search-to-post-titles-only/

    #2032689
    Miguel

    Thank you Elvin for your reply,

    I don’t need the search function to be toggle-able. I only need all the searches be based on Post titles. I really don’t want to use any 3rd party plugin, so that the overall website performance keeps unaffected.

    Hopefully you can help me with the PHP snippet for this.

    #2032703
    Elvin
    Staff
    Customer Support

    If that’s the case then you can try the PHP snippet provided on the article I’ve linked which is this one:

    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 LIKE '{$n}{$term}{$n}')";
            $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', 500, 2 );

    Here’s how to add PHP snippets – https://docs.generatepress.com/article/adding-php/

    #2032747
    Miguel

    Hi Kevin, the PHP snippet you provide is working just like I need, filtering only by Post Title. But there is an Warning message caused by the PHP snippet. Below the message:

    Warning __search_by_title_only(): Argument #2 ($wp_query) must be passed by reference, value given in wp-includes/class-wp-hook.php on line 303

    Thanks in advance for your help

    #2032768
    Elvin
    Staff
    Customer Support

    On the line function __search_by_title_only( $search, &$wp_query ) try replacing &$wp_query with $wp_query.

    Let us know how it goes.

    #2032787
    Miguel

    Thank you very much for your kind help. Now all is working perfectly.

    All the Best,
    Miguel

    #2032790
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. 😀

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.