Skip to content

Instantly share code, notes, and snippets.

@azizultex
Last active September 3, 2023 20:29
Show Gist options
  • Save azizultex/482be4202d06e031ec5adbd0a40912d3 to your computer and use it in GitHub Desktop.
Save azizultex/482be4202d06e031ec5adbd0a40912d3 to your computer and use it in GitHub Desktop.
Delete Posts After a Specific Days
function delete_posts() {
$args = array (
'post_type' => 'lionfish_locations', // post type
'nopaging' => true
);
$q = new WP_Query ($args);
while ($q->have_posts()) {
$q->the_post();
$id = get_the_ID();
$posted_time = human_time_diff(get_the_time('U'), current_time ('timestamp'));
$posted = filter_var($posted_time, FILTER_SANITIZE_NUMBER_INT); // remove 'days' from posted_time
if($posted >= 360 ) { // days to delete posts after published
wp_delete_post($id, true);
}
}
wp_reset_postdata ();
}
add_action( 'init', 'delete_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment