方法一:完全禁用 SQL_CALC_FOUND_ROWS,将代码放到 functions.php 文件中:
add_action('pre_get_posts', 'wndt_post_filter');
function wndt_post_filter($query) {
if (is_admin() or !$query->is_main_query()) {
return $query;
}
// 禁止查询 SQL_CALC_FOUND_ROWS
$query->set('no_found_rows', true);
}
function wndt_post_filter($query) {
if (is_admin() or !$query->is_main_query()) {
return $query;
}
// 禁止查询 SQL_CALC_FOUND_ROWS
$query->set('no_found_rows', true);
}
方法二、如果需要查询文章数,请使用EXPLAIN 方法代替 SQL_CALC_FOUND_ROWS,禁用 SQL_CALC_FOUND_ROWS。这里使用 EXPLAIN 方法。将代码放在 functions.php 文件中:
if ( ! function_exists( 'zjck_set_no_found_rows' ) ) {
/**
* 设置WP_Query的 'no_found_rows' 属性为true,禁用SQL_CALC_FOUND_ROWS
*
* @param WP_Query $wp_query WP_Query实例
* @return void
*/
function zjck_set_no_found_rows(\WP_Query $wp_query)
{
$wp_query->set('no_found_rows', true);
}
}
add_filter( 'pre_get_posts', 'zjck_set_no_found_rows', 10, 1 );
if ( ! function_exists( 'zjck_set_found_posts' ) ) {
/**
* 使用 EXPLAIN 方式重构
*/
function zjck_set_found_posts($clauses, \WP_Query $wp_query)
{
// Don't proceed if it's a singular page.
if ($wp_query->is_singular()) {
return $clauses;
}
global $wpdb;
$where = isset($clauses['where']) ? $clauses['where'] : '';
$join = isset($clauses['join']) ? $clauses['join'] : '';
$distinct = isset($clauses['distinct']) ? $clauses['distinct'] : '';
$wp_query->found_posts = (int)$wpdb->get_row("EXPLAIN SELECT $distinct * FROM {$wpdb->posts} $join WHERE 1=1 $where")->rows;
$posts_per_page = (!empty($wp_query->query_vars['posts_per_page']) ? absint($wp_query->query_vars['posts_per_page']) : absint(get_option('posts_per_page')));
$wp_query->max_num_pages = ceil($wp_query->found_posts / $posts_per_page);
return $clauses;
}
}
add_filter( 'posts_clauses', 'zjck_set_found_posts', 10, 2 );
/**
* 设置WP_Query的 'no_found_rows' 属性为true,禁用SQL_CALC_FOUND_ROWS
*
* @param WP_Query $wp_query WP_Query实例
* @return void
*/
function zjck_set_no_found_rows(\WP_Query $wp_query)
{
$wp_query->set('no_found_rows', true);
}
}
add_filter( 'pre_get_posts', 'zjck_set_no_found_rows', 10, 1 );
if ( ! function_exists( 'zjck_set_found_posts' ) ) {
/**
* 使用 EXPLAIN 方式重构
*/
function zjck_set_found_posts($clauses, \WP_Query $wp_query)
{
// Don't proceed if it's a singular page.
if ($wp_query->is_singular()) {
return $clauses;
}
global $wpdb;
$where = isset($clauses['where']) ? $clauses['where'] : '';
$join = isset($clauses['join']) ? $clauses['join'] : '';
$distinct = isset($clauses['distinct']) ? $clauses['distinct'] : '';
$wp_query->found_posts = (int)$wpdb->get_row("EXPLAIN SELECT $distinct * FROM {$wpdb->posts} $join WHERE 1=1 $where")->rows;
$posts_per_page = (!empty($wp_query->query_vars['posts_per_page']) ? absint($wp_query->query_vars['posts_per_page']) : absint(get_option('posts_per_page')));
$wp_query->max_num_pages = ceil($wp_query->found_posts / $posts_per_page);
return $clauses;
}
}
add_filter( 'posts_clauses', 'zjck_set_found_posts', 10, 2 );
本文是全系列中第5 / 14篇:优化加速
- 利用七牛云对象存储+CDN服务,给WordPress网站静态文件加速
- 推荐几款WordPress清理优化插件
- 清理不必要WordPress数据优化数据库插件:Easy WP Cleaner
- wordpress如何开启Memcached缓存加速
- 优化WordPress文章过多导致网站卡顿
- 让WP Rocket速度更快的小技巧 :更适合Nginx服务器
- 清理WordPress中数据库冗余数据的方法
- wordpress删除文章修订版本和自动草稿而残留的数据
- WordPress图像优化插件:Plus WebP
- lnmp 开启 Opcache 脚本缓存提高 php 性能
- 使用OpenLiteSpeed+LiteSpeed cache优化加速Wordpress网站
- WordPress设置QUIC.Cloud免费CDN图文教程
- WordPress禁止上传图片生成多个尺寸版本
- WordPress开启Redis缓存加速