X

Limit search to post titles only

Adding this snippet to the functions.php of your wordpress theme will limit search to post titles only.



Code Snippet:


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 );

Comments  Leave a Reply

  1. This is really nice! Thank you!

  2. du lich singapore January 20, 2014 at 7:51 pm

    code: add_filter( ‘posts_search’, ‘__search_by_title_only’, 500, 2 );

    2 params: 500, 2 . What is mean ?

  3. du lich singapore January 20, 2014 at 7:46 pm

    add_filter( ‘posts_search’, ‘__search_by_title_only’, 500, 2 );
    2 params 500, 2. What is mean ?

  4. I don’t understand much of PHP, do I need to put something after and before that code?

  5. This is awesome… I’m wondering how to add this code to back-end search. I want title-wise search for wp back-end too.. 🙂

    Thank you! 🙂

  6. Hi,
    That was truly awesome.
    You just solved my problem with the least bit effort from my side.
    Thanks a lot.

  7. disqus_JuU9dzgzNc January 18, 2013 at 2:27 pm

    This is great! I have 2 search forms on my website. Is it possible to only apply this function to 1 of the search forms

  8. Hey Mister!
    you rule man, thanx alot

  9. Thnx Kevin … exactly the code i was looking for …

    1. Cool glad to hear I could help out.

  10. I’m new (aka “I suck”) at both PHP and WordPress.  Do I just drop this code in arbitrarily or is there a specific place where it needs to go or replace some code that’s already there?

    I put this at the bottom of the functions.php page and my screen was white, so I removed it.

    1. Ok, I was adding this to the WordPress functions.php file and not the theme one.  It works like a charm.

  11. Great code! This really cleans up the wordpress search.  Is there a way to include the first X words of the post? Maybe with the substr function?
    $content = get_the_content();
    $content = strip_tags($content);
    echo substr($content, 0, 50);

    1. In your search results template you could trim the amount of text displayed on use the excerpt template tag for a shorter result without any hacking.

  12. This is great! Something I’d like to see is a search tweak that limits the results to titles and tags. That way I have the freedom to title my posts whatever I want without worrying about needing to include keyword search terms. 

    1. Glad to hear you like the snippet! If you are going to take things that far tags, etc, I would look into the plugin “search everything” it has a lot of options that you can configure.

  13. doesn’t work! 🙁

    1. what version of wordpress are you currently running? I test everything to make sure it works before I post a snippet. Ill take a look at this again just to make sure.

      1. 3.2.1

        this is the output of $search:

        AND ( AND (((wpmtx_posts.post_title LIKE ‘%alice%’) OR
        (wpmtx_posts.post_content LIKE ‘%alice%’)) AND ((wpmtx_posts.post_title
        LIKE ‘%cooper%’) OR (wpmtx_posts.post_content LIKE ‘%cooper%’)))
        (.post_title LIKE ‘%alice%’) AND (.post_title LIKE ‘%cooper%’) OR
        (.post_title LIKE ‘%alice cooper%’))

        1. made a small update see if that fixes things,

        2. Yes! Now works! Thanks you very much!!!

      2. 3.2.1

        this is the output of $search:

        AND ( AND (((wpmtx_posts.post_title LIKE ‘%alice%’) OR
        (wpmtx_posts.post_content LIKE ‘%alice%’)) AND ((wpmtx_posts.post_title
        LIKE ‘%cooper%’) OR (wpmtx_posts.post_content LIKE ‘%cooper%’)))
        (.post_title LIKE ‘%alice%’) AND (.post_title LIKE ‘%cooper%’) OR
        (.post_title LIKE ‘%alice cooper%’))

  14. 29 Wordpress Tweaks to Improve Posts and Pages October 18, 2011 at 9:11 am

    […] [Source: WPSNIPP] […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!