0

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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