Tag Archive for '计划任务'

让 WordPress 自动删除 Post Revisions

Posted on 2008-11-18 in Blog RelatedComments

貌似在 wp-config.php 中加入 define(’WP_POST_REVISIONS’, false); 来禁用 WordPress 的日志修订功能,post revision 还是会产生。gohsy 同学写了个插件 Revision Manager 来清理 post revision,不过个人觉得手动清理还是不够方便,决定利用 WordPress 的计划任务功能(WP_Cron)偷偷懒。

不想为了这小小的功能而多添加一个插件,所以在主题目录下的 functions.php 文件添加了以下代码:

function delete_post_revisions() {
  global $wpdb;

  // Also need to delete the post meta and term relationships
  $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'revision')");
  $wpdb->query("DELETE FROM {$wpdb->term_relationships} WHERE object_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'revision')");

  // Delete the post revisions
  $wpdb->query("DELETE FROM {$wpdb->posts} WHERE post_type = 'revision'");
}

// Register the event
add_action('delete_post_revisions_event', 'delete_post_revisions');
if (!wp_next_scheduled('delete_post_revisions_event')) {
  wp_schedule_event(time(), 'daily', 'delete_post_revisions_event');
}

Continue reading…