Prevent yoast high mysql load on huge websites
run this command every 12 hours
/usr/bin/php "/home/user/public_html/wp-truncate-indexables.php" > /dev/null 2>&1
<?php | |
global $wpdb; | |
ignore_user_abort( true ); | |
define( "WP_USE_THEMES", false ); | |
define( "DONOTCACHEPAGE", true ); | |
ini_set("memory_limit","6G"); | |
if(!function_exists("tnl_is_cli")){ | |
function tnl_is_cli() | |
{ | |
if ( defined("STDIN") ) | |
{ | |
return true; | |
} | |
if ( php_sapi_name() === "cli" ) | |
{ | |
return true; | |
} | |
if ( array_key_exists("SHELL", $_ENV) ) { | |
return true; | |
} | |
if ( empty($_SERVER["REMOTE_ADDR"]) and !isset($_SERVER["HTTP_USER_AGENT"]) and count($_SERVER["argv"]) > 0) | |
{ | |
return true; | |
} | |
if ( !array_key_exists("REQUEST_METHOD", $_SERVER) ) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
if(!tnl_is_cli()){ | |
exit(400); | |
} | |
require( dirname( __FILE__ ) . "/wp-blog-header.php" ); | |
$tablesToTruncate = ["yoast_indexable","yoast_indexable_hierarchy","yoast_seo_meta"]; | |
foreach($tablesToTruncate as $tableName){ | |
// first check | |
if(in_array($tableName,$tablesToTruncate)){ | |
//second check | |
if($tableName === "posts" or $tableName === "postmeta"){ continue; } | |
$table = $wpdb->prefix . $tableName; | |
$delete = $wpdb->query("TRUNCATE TABLE $table"); | |
if($delete){ echo "TRUNCATE {$table} OK \n"; } | |
} | |
} |