// Disable SQL_CALC_FOUND_ROWS for pagination add_filter('found_posts_query', function($sql){ return preg_replace('/SQL_CALC_FOUND_ROWS/i', '', $sql); }); // Custom fast COUNT(*) for pagination add_filter('found_posts', function($found_posts, $query){ global $wpdb; if (!$query->is_main_query() || !$query->is_archive() && !$query->is_home()) { return $found_posts; } $cache_key = 'fast_count_' . md5($query->request); $cached = wp_cache_get($cache_key, 'fast_query'); if ($cached !== false) { return $cached; } // Convert main query to COUNT(*) $count_query = "SELECT COUNT(*) FROM ({$query->request}) AS t"; $total = (int) $wpdb->get_var($count_query); // Cache for 5 minutes wp_cache_set($cache_key, $total, 'fast_query', 300); return $total; }, 10, 2);