“Call to member function format() on a non-object” means that $formatted_date
is not an object.
Why do you need 2 functions for this? Can’t you just check the post meta for your custom field in the save routine? Especially with such a late priority the custom field should already be saved.
function acf_set_expiry($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// don't run the echo if the function is called for saving revision.
if ( wp_is_post_revision( $post_id ) )
return $post_id;
//'M j, Y' is the format my ACF date field is outputting - can be differ from each setup!
// get the date in this function
$date = get_post_meta($post_id, 'cf_end_date', true);
$formatted_date = DateTime::createFromFormat('m-d-Y', $date);
$month = intval($formatted_date->format('m'));
$day = intval($formatted_date->format('d'));
$year = intval($formatted_date->format('y'));
//I am not using time in my ACF field, so I am setting it manually to the end of the day.
$hour = 23;
$minute = 59;
$opts = array();
$ts = get_gmt_from_date("$year-$month-$day $hour:$minute:0",'U');
// Schedule/Update Expiration
//$opts['expireType'] = 'draft';
$opts['id'] = $id;
_scheduleExpiratorEvent($id,$ts,$opts);
}
add_action('save_post', 'acf_set_expiry',100);
I would suggested perhaps adding some conditional checks to see if the $date
exists before trying to turn it into an object. Additionally you should add permissions checks as seen in the codex metabox examples.