Skip to content

Instantly share code, notes, and snippets.

@tallesairan
Last active September 22, 2021 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tallesairan/13e28a686b6467766cfd39acd2a1bfca to your computer and use it in GitHub Desktop.
Save tallesairan/13e28a686b6467766cfd39acd2a1bfca to your computer and use it in GitHub Desktop.
Truncate yoast_indexable crontab to solve perfomance issues

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"; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment