我正在尝试使用WP Frontend User为后期保存添加一个挂钩
当表单保存并在“save\\u post”功能中创建帖子时,我试图找到自定义字段“cf\\u end\\u date”的值并将其分配给其他内容
我无法足够快地获取自定义字段“cf\\u end\\u date”值。如果我转到后端并编辑现有帖子,那么该功能工作正常。
这是我在函数中的代码。php
// assign post expiration
function acf_set_expiry($post_id){
$date = get_post_meta($post_id, \'cf_end_date\', true);
}
//modified update_post_meta function
function expirationdate_update_post_meta_acf($id, $date) {
// don\'t run the echo if this is an auto save
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
// don\'t run the echo if the function is called for saving revision.
$posttype = get_post_type($id);
if ( $posttype == \'revision\' )
{
return;
} else {
//\'M j, Y\' is the format my ACF date field is outputting - can be differ from each setup!
$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);
SO网友:helgatheviking
“对非对象调用成员函数format()”表示$formatted_date
不是对象。
为什么需要2个函数来实现此功能?您不能在保存例程中检查自定义字段的post meta吗?特别是对于这样一个延迟的优先级,应该已经保存了自定义字段。
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);
我建议可能添加一些条件检查,看看
$date
在尝试将其转换为对象之前存在。此外,您应该添加权限检查,如
codex metabox examples.