我正计划启动一个基于报纸的网站(每日版报纸)。我正在 WP 上构建网站。
现在,根据我的客户的要求,需要在 30 天后从 WP DB 中完全删除新闻项目(在这种情况下为 WP 帖子)。他们甚至不需要存档,因为他们不想使数据库膨胀。相反,他们将在其本地系统中保留文件/帖子的计划备份,但希望 WP 完成从 WP 安装/数据库中删除这些帖子。
有可能吗?如果是这样,如何在 WP 上执行此操作。
你确定你需要 WordPress 吗?强迫 WordPress 进入这样一个持续的过程似乎过于复杂。
我建议只将以下 SQL 语句放在cron作业中(如单个 .php 文件),其余部分将自行处理。
DELETE FROM wp_posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 30 DAY);
Run Code Online (Sandbox Code Playgroud)
更新
使用 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);"));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10430 次 |
| 最近记录: |