Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active July 16, 2023 23:31
Show Gist options
  • Save wpmudev-sls/ff1dc127a3897c53cb436587bd1afa9e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/ff1dc127a3897c53cb436587bd1afa9e to your computer and use it in GitHub Desktop.
[Hummingbird Pro] Fix OOM error when a huge amount of wphb_minify_group posts are present
<?php
/**
* Plugin Name: [Hummingbird Pro] Fix OOM error when a huge amount of wphb_minify_group posts are present
* Description: Limits the query that retrieves the minify groups to 10 to prevent an excessive resource usage on the server
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-4065, SLS-4071
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
/*
It is important to determine that the problem is actually caused by the amount wphb_minify_group posts in database.
The symptoms are the following:
- General outgaes
- Impossibility to open the HB Dashboard, the AO page or the Plugin Health page
- Impossibility to deactivate the plugin
- This PHP error: "Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 757760 bytes) in /wp-includes/wp-db.php on line 2135"
The definitive way to check this is by running a database query:
SELECT count(*) FROM wp_posts WHERE post_type = 'wphb_minify_group';
*/
add_action( 'plugins_loaded', function() {
if ( ! defined( 'WPHB_VERSION' ) ) {
return; // Hummingbird is not installed/enabled.
}
add_action( 'pre_get_posts', function( $query ) {
if ( ! empty( $query->query['post_type'] ) && 'wphb_minify_group' === $query->query['post_type'] ) {
if ( -1 === $query->query['posts_per_page'] ) {
$query->query['posts_per_page'] = 10;
$query->query_vars['posts_per_page'] = 10;
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment