![](/img/trans.png)
[英]Wordpress ACF - Auto delete posts if ACF date field is older than current date
[英]Delete posts older than 2 years in wordpress site
我想以编程方式删除我的WordPress网站上超过两年的所有帖子。 大约有37000个帖子。 需要帮助将其批量删除。 怎么可能呢? 最简单的删除方法是什么?
运行此SQL查询
DELETE FROM `wp_posts` WHERE YEAR(CURDATE()) - YEAR(`post_date`) >= 2;
尝试使用此代码。请阅读我在代码中的注释以获取更多理解。 函数将进入您的functions.php
function get_delete_old_post() {
// WP_Query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => array( 'post' ), //post type if you are using default than it will be post
'posts_per_page' => '-1',//fetch all posts,
'date_query' => array(
'column' => 'post_date',
'before' => '-2 years'
)//date query for before 2 years you can set date as well here
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
//delete post code
//wp_trash_post( get_the_ID() ); use this function if you have custom post type
wp_delete_post(get_the_ID(),true); //use this function if you are working with default posts
}
} else {
// no posts found
return false;
}
die();
// Restore original Post Data
wp_reset_postdata();
}
add_action('init','get_delete_old_post');
您可能只是喜欢这个插件(我写的)“自动修剪帖子”,它会根据分类法+年龄在2年后删除帖子。 https://wordpress.org/plugins/auto-prune-posts/
当您只是“从...删除”时,您将至少在postmeta表中得到孤立的数据。 我想你不想那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.