0

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;
    }
}
2
  • Its not clear what you are asking for help with. What isn't working or what is it that you are stuck on? Commented Oct 18, 2017 at 2:40
  • I can successfully run the Cron Job now however it will not delete the post that is under the CPT 'hp_dual_credit'. I have even tried removing all of the time logic and just trying to delete it with wp_delete_post but was unsuccessful
    – bilcker
    Commented Oct 18, 2017 at 14:36

1 Answer 1

1

Ok, so it looks like you're having a problem with the publish time being a unix timestamp, but your current time is not, it's just returning the hours.

$publish_time = get_the_time('U'); // Returns our $publish time as a Unix timestamp
$delete_time = $publish_time + 43200; // 60 sec * 60 min * 12 hrs = 43,200 sec
$current_time = time(); // time is a the current time in a Unix timestamp

if ( $current_time >= $delete_time ) {
    wp_delete_post(get_the_ID(), true);
}
3
  • Thanks, that makes more sense. However it is still not deleting the post. I altered the function to send me an email on When the Cron Job runs and that worked so I know it's not that. I also removed the time piece to see if it would delete the post and it didn't work. So now my thoughts are that I am not targeting the post properly. Do you need to pass any $post variable through the function perhaps?
    – bilcker
    Commented Oct 20, 2017 at 15:00
  • Did you try emailing yourself the ID of the post? Or try trashing the post without force deleting it? Also, be sure to run a wp_reset_query after your custom WP Query while loop is done.
    – socki03
    Commented Oct 20, 2017 at 16:18
  • Ok so I still wasn't getting the id and in the cron manager plugin when I forced it to execute kept getting an error, so mulling over my code I found. while($expiry_query->have_posts()) : which should be $expire_query and it is now working!. Thanks for your help
    – bilcker
    Commented Oct 20, 2017 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.