Search posts exclusively by their titles

If you’re still using default WordPress search engine, sooner or later you will want to modify how it works. While there’s nothing wrong in the way it searches for your posts, you might want to limit the search to titles.

Instead of going through all of the content, this little function will search only through post titles. In some cases, this might really help you get cleaner and faster search results:

  1. Open functions.php
  2. Copy and paste this code:
  3. 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($wpdb->esc_like($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);
  4. Save changes

After the changes, you can open your website and start a new search. To be sure everything’s working as it should, you should try searching for a word you know is in one of your post titles which should return the post in the results page. If you try to search for a word or a complete term that isn’t contained in any title whic you have published, search results will come out empty.

And that’s actually all there is. If you ever decide you want the old search back, simply erase or comment out the function.

Enjoy your searching. Oh, if you want to further improve your search results, you might be interested in excluding all or only specific pages from your search results.


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *