cron job to auto delete posts of a specific post type older than x days
-
Hello,
I want to delete all posts of a specific post type (in this case “vfb_entry”) that are older than 60 days. The cron job should run once a day.
I have the following code and would like to ask you guys if it is correct:
// Cron Job to Delete VFB Entries older than 60 days if(!wp_next_scheduled( 'remove_old_vfb_entries')){ wp_schedule_event(time(), 'daily', 'remove_old_vfb_entries'); } add_action('remove_old_vfb_entries', 'myd_remove_old_vfb_entries'); // Build the function function myd_remove_old_vfb_entries(){ global $wpdb; // Set max post date and post_type name $date = date("Y-m-d H:i:s", strtotime('-60 days')); $post_type = 'vfb_entry'; // Query post type $query = " SELECT ID FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = 'publish' AND post_author = '1' AND post_date < '$date' ORDER BY post_modified DESC "; $results = $wpdb->get_results($query); foreach($results as $post){ // Let the WordPress API clean up the entire post trails wp_delete_post( $post->ID, true); } }
Thank you!
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘cron job to auto delete posts of a specific post type older than x days’ is closed to new replies.