0

I'm trying to delete certain posts (of a custom post type) automatically based on a comparison between the current time and a number of hours previous to now, for example 24 hours. The custom post type is appointments, and if an appointment is less than 24 hours in the future, and holds a meta_value of 'available' in the meta_key 'app_status', I want to delete it. The below code is what I'm trying, but it's not working. Posts that I have set with an app_date_start of less than 24 hours from now do not delete. I've tried different hooks to attach it to with no luck as well.

function delete_unclaimed_apps () { $avail = array( 'post_type' => 'appointment', 'meta_key' => 'app_status', 'meta_value' => 'available', ); $availapps = get_posts($avail); $now = (time()-(7*60*60)); $cutoff = (get_field('minimum_reserve_notice', 'options') * 60 * 60); foreach ($availapps as $post) { setup_postdata( $post ); $appid = get_the_ID(); if (get_post_meta($appid, 'app_date_start', true)) { if (get_post_meta($appid, 'app_date_start', true) < ($now + $cutoff)) { wp_delete_post($appid, false); } } wp_reset_postdata(); } } add_action('wp_enqueue_scripts', 'delete_unclaimed_apps'); 
5
  • try with init hook . code will be like this add_action('init', 'delete_unclaimed_apps'); Commented Aug 2, 2014 at 5:22
  • Can you check that get_field('minimum_reserve_notice', 'options') is returning the correct value? Also change this line little bit if ( (int) get_post_meta($appid, 'app_date_start', true) < ($now + $cutoff)) Commented Aug 2, 2014 at 5:51
  • The variable definitely has the correct value, I tested it. Even when I hard-code the value in it doesn't seem to work. I tried adding (int) as well, with now luck. I did a little more testing and the function is firing correctly when I first create a new post. If I give it a past date when creating it, it will automatically delete it, just not any other time. Commented Aug 2, 2014 at 6:03
  • What is the date format stored in app_date_start()? Commented Aug 2, 2014 at 8:01
  • I figured it out after deciding to re-write the function from scratch, which I've found can sometimes help find something I've missed, which I did. Answer below, and thanks for your help attempts! Commented Aug 8, 2014 at 6:08

1 Answer 1

0

Here is what I ended up with: Basically, I had the logic of the timing at the bottom wrong. Figuring out times and dates and what note is hard...

add_action('init', 'delete_unclaimed_apps'); function delete_unclaimed_apps() { $availapps = get_posts(array( 'post_type' => 'appointment', 'post_status' => 'publish', 'meta_query' => array ( array( 'key' => 'app_status', 'value' => 'available', ), ), )); foreach ($availapps as $post) { $timestamp = get_post_meta($post->ID, 'app_timestamp', true); $now = time() - (7*60*60); $cutoff = (get_field('minimum_reserve_notice', 'options') * 60 * 60); if ($now > ($timestamp - $cutoff)) { wp_trash_post($post->ID); } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.