WordPress占用资源多,很多原因是因为搜索占用的内存大。他一般默认的是全文搜索的,这里我们可以设置成只搜索标题关键字是否符合搜索要求的。这里,我们可以进行修改。
方法很简单,直接将下面的代码复制到主题下的 functions.php 文件内即可。
/**
* https://www.laozuo.org
*/
function wp_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', 'wp_search_by_title_only', 500, 2 );