We want to delete old post in easy way, Add the following code to your current themes functions.php file and reload your page and see your post may have been deleted.
‘ids’,
‘post_type’ => ‘post’, //specify your post type here.
‘posts_per_page’ => ‘-1’,
‘date_query’ => array(
‘column’ => ‘post_date’,
‘before’ => date( “Y-m-d H:i:s”, strtotime( ‘-1 year’ ) ), //specify your time here.
)
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
wp_delete_post(get_the_ID(),true); //use this function if you are working with default posts
}
} else {
echo ‘no posts found’;
return false;
}
die();
wp_reset_postdata();
}
?>