Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Does anyone have any suggestions on how to limit the search in the WP admin area to just post titles? It's very frustrating to want to pull up a specific page, and get hundreds of results that have that word in the post content.

share|improve this question
add comment

1 Answer 1

This is possible by altering the search query via the posts_search hook.

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php#L2202

A default search query will look like this:

..AND (((wp_posts.post_title LIKE \'%search terms%\') OR (wp_posts.post_content LIKE \'%search terms%\')))

We need to remove the post content search, a regular expression should be enough.

add_filter( 'posts_search', 'wpse_45153_filter_search', null, 2 );
function wpse_45153_filter_search( $search, $a_wp_query ) {
    if ( !is_admin() ) return $search; // work only in the dashboard

    $search = preg_replace( "# OR \(.*posts\.post_content LIKE \\'%.*%\\'\)#", "", $search );

    return $search;
}

Note that the regular expression has not been thoroughly tested and may have edge cases when other plugins or themes alter hook into here.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.