在wordpress中特定天数后自动删除已发布的帖子

时间:2014-02-10 11:57:29

标签: wordpress

我打算开办一家报纸网站(每日报纸)。我正在WP上建站点。

现在,根据我的客户的需求,新闻项目(本例中的WP帖子)需要在30天后从WP DB中完全删除。他们甚至不需要存档,因为他们不想膨胀数据库。相反,他们将在本地系统中保留文件/帖子的计划备份,但希望WP完成从WP安装/ DB中删除这些帖子。

有可能吗?如果是这样,如何在WP上执行此操作。

2 个答案:

答案 0 :(得分:4)

你确定你需要WordPress吗?迫使WordPress进入这样一个持续的过程似乎过于复杂。

我建议将以下SQL语句放在cron作业中(如单个.php文件),其余的将由自己处​​理。

DELETE FROM wp_posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 30 DAY);

Here's a good rundown on using cron to automatically do these kinds of this for you

<强>更新

使用WordPress调度功能,我们可以提供一个类似于cron的基于时间的删除后删除计划。这不是一个真正的cron工作,因为它只有在一个人在30天之后访问该网站时才会删除我们的帖子,而不是在30天之后删除我们的帖子而不管访问者。但是,它是我们通过WordPress得到的最接近的,在这种情况下,结果将是相同的。将以下内容添加到functions.php或您的插件文件中。

/**
 * Add monthly interval to the schedules (since WP doesnt provide it from the start)
 */
add_filter('cron_schedules','cron_add_monthly');
function cron_add_monthly($schedules) {
$schedules['monthly'] = array(
  'interval' => 2419200,
  'display' => __( 'Once per month' )
);
return $schedules;
}
/**
 * Add the scheduling if it doesnt already exist
 */
add_action('wp','setup_schedule');
function setup_schedule() {
  if (!wp_next_scheduled('monthly_pruning') ) {
    wp_schedule_event( time(), 'monthly', 'monthly_pruning');
  }
}
/**
 * Add the function that takes care of removing all rows with post_type=post that are older than 30 days
 */
add_action( 'monthly_pruning', 'remove_old_posts' );
function remove_old_posts() {
  global $wpdb;
  $wpdb->query($wpdb->prepare("DELETE FROM wp_posts WHERE post_type='post' AND post_date < DATE_SUB(NOW(), INTERVAL 30 DAY);"));
}

答案 1 :(得分:1)

您可以安装WP-post expirator并将其默认设置为之后删除帖子的天数。它也适用于WP-automatic https://srd.wordpress.org/plugins/post-expirator/

相关问题