Site logo

Archived topic

How to search only by Post Title?

7 replies · Started by Miguel on November 29, 2021

Viewing posts 1–8 of 8

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

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/

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.

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/

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

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

Let us know how it goes.

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

All the Best,
Miguel

No problem. Glad to be of any help. :D

This archived topic is closed to new replies.