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');
app_date_start()?