gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:21:13 26 4
gpt4 key购买 nike

我打算创建一个基于报纸的网站(日报)。我正在 WP 上构建网站。

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

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

最佳答案

您确定为此需要 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);"));
}

关于wordpress - 在 wordpress 中特定天数后自动删除已发布的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com