How to Limit WordPress Search to Post Titles Only

WPTurbo » Snippets » How to Limit WordPress Search to Post Titles Only
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Ever needed to narrow down your search results on your WordPress website to only include post titles? With a sea of information flooding your website, searching through every piece of data can become overwhelming and inefficient. This article will guide you through the process of limiting your search functionality to post titles only, streamlining the search experience for your users.

					function wpturbo_search_by_title_only( $search, $wp_query ) {
    if ( ! empty( $search ) && ! empty( $wp_query->query_vars['s'] ) ) {
        global $wpdb;

        $search = ' AND ' . $wpdb->posts . '.post_title LIKE '' . esc_sql( like_escape( $wp_query->query_vars['s'] ) ) . '%'';
    }

    return $search;
}
add_filter( 'posts_search', 'wpturbo_search_by_title_only', 10, 2 );
				

The function wpturbo_search_by_title_only($search, $wp_query) is designed to limit WordPress search to post titles exclusively.

In the function, we are passing two arguments, $search and $wp_query. The $search parameter contains the search SQL for the search. This SQL snippet can be further modified to fine-tune your search parameters. On the other hand, $wp_query is an instance of the WP_Query class. It provides access to all the query variables WordPress uses to extract information from the database.

The function starts by checking if neither $search nor $wp_query->query_vars['s'] are empty, $wp_query->query_vars['s'] stores the current search string provided by the user. This condition is important to ensure that some search criteria have been input before trying to modify the $search parameter. Without this condition, the function might run even when there’s no active search, leading to potential issues or errors as it would be trying to modify a search that doesn’t exist.

Next, we see the global $wpdb; declaration, which brings the $wpdb global object into scope so we can interact with the WordPress database.

Now comes the main part of this function, the modification of the $search string:

$search = ' AND ' . $wpdb->posts . '.post_title LIKE '' . esc_sql( like_escape( $wp_query->query_vars['s'] ) ) . '%'';

Here we are telling WordPress to add a condition to the SQL query it uses to get posts. This condition narrows down the search results to only include posts whose titles contain the search term.

To clean the search string we apply esc_sql( like_escape( $wp_query->query_vars['s'] ) ). This ensures that the input is safe to use in our SQL query and any special SQL characters in the search terms are properly escaped.

Finally, the function returns the modified $search parameter to be used in the WordPress search.

The code concludes with the add_filter function, which tells WordPress to use our custom function, wpturbo_search_by_title_only, to modify WordPress’s default search behavior. This is hooked into the posts_search filter, which is specifically designed for modifying the search SQL query. We set the priority to 10 and number of accepted arguments to 2 (which are $search and $wp_query respectively). This is done to ensure that our function only runs after other functions hooked into the posts_search filter have run.

Register an account to save your snippets or go Pro to get more features.