Search Only Post Titles in WordPress

Advanced Guides Code Snippets Coding Blog Content Management PHP PHP Debugging PHP Development PHP Snippets Theme Customization Theme Development Woocommerce Wordpress WordPress Code Snippets WordPress Development WordPress Functions WordPress Theme Development WordPress Tutorials

Search Only Post Titles in WordPress Tutorial/Guide

If you want your WordPress search to check only post titles and ignore content, here's a quick method using the posts_search filter in your theme:

  1. Navigate to your theme's directory and open the functions.php file.
  2. Add the following code snippet:
function limit_search_to_post_titles($search, $wp_query) {
    if (is_search() && !is_admin()) {
        $search_terms = $wp_query->query_vars['s'];
        
        if (!empty($search_terms)) {
            global $wpdb;
            $search = $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like($search_terms) . '%');
        }
    }
    return $search;
}
add_filter('posts_search', 'limit_search_to_post_titles', 10, 2);

That’s it! Now your search will filter by post titles only, excluding body content and pages.    

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.