Last active
September 3, 2023 20:29
-
-
Save azizultex/482be4202d06e031ec5adbd0a40912d3 to your computer and use it in GitHub Desktop.
Delete Posts After a Specific Days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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