wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Limit search to post titles only

wordpress snippet

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

snippet :  PHPcopy
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 );
source →
  • Pingback: 29 Wordpress Tweaks to Improve Posts and Pages

  • davidsky

    doesn’t work! :(

    • http://wpsnipp.com Kevin Chard

      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.

      • davidsky

        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%’))

        • http://wpsnipp.com Kevin Chard

          made a small update see if that fixes things,

        • davidsky

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

      • davidsky

        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%’))

  • http://jaledwith.com/ Andrew

    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. 

    • http://wpsnipp.com Kevin Chard

      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.

  • Seth

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

    • http://wpsnipp.com Kevin Chard

      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.

  • Matt

    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.

    • Matt

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

  • Web Php 03

    Thnx Kevin … exactly the code i was looking for …

    • http://wpsnipp.com Kevin Chard

      Cool glad to hear I could help out.

  • steve

    Hey Mister!
    you rule man, thanx alot

  • disqus_JuU9dzgzNc

    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

  • Satyaki

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

  • Silver City

    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! :)

  • MR_ShockWave

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

  • http://dulichsingapore.net.vn/Du-lich-Singapore-Malaysia-c2.html du lich singapore

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

  • http://dulichsingapore.net.vn/Du-lich-Singapore-Malaysia-c2.html du lich singapore

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

    2 params: 500, 2 . What is mean ?

jqsnipp.com | we just launched jquery snippets website