Limit search to post titles only in wordpress
default wordpress search function to search content by keyword in the title, the content … nhuwng I find it does not work properly on what they want, often just search by title is enough, but the result, I do not know where it came from but a lot of results, most of them irrelevant from find his keys.
in this article I refer to you a way to fix this, do not lose your time anywhere, only takes about 1 minute from now only.
you copy the following code to put in my functions.php file in the theme that you are okay to use.
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 );
finished, so you try to see if there is not? too easy, right?