Skip to content

Instantly share code, notes, and snippets.

@Pross
Last active December 14, 2015 10:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pross/5071147 to your computer and use it in GitHub Desktop.
Save Pross/5071147 to your computer and use it in GitHub Desktop.
Delete transients older than 7 days.
<?php
class PurgeTransients {
var $expire = '7 days';
function __construct(){
add_action( 'admin_init', array( &$this, 'init' ) );
}
function init(){
$this->purge( $this->expire );
}
function purge( $older_than ) {
global $wpdb;
$older_than_time = strtotime('-' . $older_than);
if ( $older_than_time > time() || $older_than_time < 1 ) {
return false;
}
$transients = $wpdb->get_col(
$wpdb->prepare( "
SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name
FROM {$wpdb->options}
WHERE option_name LIKE '\_transient\_timeout\__%%'
AND option_value < %s
", $older_than_time )
);
if( ! is_array( $transients ) )
return;
$option_names = array();
foreach( $transients as $transient ) {
$option_names[] = '_transient_' . $transient;
$option_names[] = '_transient_timeout_' . $transient;
}
if ( ! empty( $option_names ) ) {
foreach( $option_names as $option )
$result = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name = '{$option}'" );
}
}
}
new PurgeTransients;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment