Bạn hãy thêm đoạn code sau vào file functions.php
add_filter('posts_search','tmdev_custom_search_wordpress', 500, 2);
function tmdev_custom_search_wordpress( $search, $wp_query )
{
global $wpdb;
if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$n = '';
$search = $searchand = '';
$term_post_content = implode(" ", $q['search_terms']);
$term_post_content = esc_sql( like_escape( $term_post_content ) );
foreach ( (array) $q['search_terms'] as $key1 => $term ) {
$term = esc_sql( like_escape( $term ) );
if( $key1 == 0 ){
$term = $term.'-';
}
if( $key1 > 0 ){
$term = '-'.$term;
}
$search .= "{$searchand}($wpdb->posts.post_name REGEXP '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
$search .= " OR ($wpdb->posts.post_content REGEXP '{$n}{$term_post_content}{$n}')";
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}