I know there are plugins for this but none allow me do expire them automatically without manually picking a date inside of the post. I have many users who post to different CPT's and I want to delete them within 12 hours (or so) of being posted without the user manually setting it. I have been researching best ways to do this and have been trying to piece together different suggestions to accomplish my goal. I am hoping someone can point me in the right direction.
I know I am not there but I am also at a loss as to where to go from here. I appreciate any input you can give.
For this example I chose to just start with a specific custom pout type for simplicity.
To start I built a WP-Cron Job based off of https://www.elegantthemes.com/blog/tips-tricks/how-to-add-cron-jobs-to-wordpress
functions.php
add_action('wp','alerts_cron');
function alerts_cron(){
if(!wp_next_scheduled('hpsts_alerts_cron')){
wp_schedule_event_time(time(), 'hourly', 'hpsts_alerts_cron');
}
}
Next My action function, I used this post as a starting point
add_action('hpsts_alerts_cron','expire_cpt_alert');
function expire_cpt_alert(){
$args = array (
'post_type' => 'hp_dual_credit',
'post_status' => 'publish'
);
$expire_query = new WP_Query($args);
if($expire_query->have_posts()) {
while($expiry_query->have_posts()) : $expire_query->the_post();
//This is where I start to get lost, I know I want to expire after 12 hours however if my Cron runs hourly I could test better if this was
$publish_time = get_the_time('U');
$delete_time = $publish_time + 3600;
$current_time = date('H');
if($current_time >= $delete_time){
wp_delete_post(get_the_ID(), true);
}
endwhile;
}
}