问题描述
我想将常规 wordpress 帖子的发布日期保存到自定义元字段中。有没有办法根据下面的场景编写函数?
需求场景: 当我保存/创建帖子时,某些函数将使用当前月份和年份,然后将这些数据保存到名为 custom_month 和 custom_year 的自定义元字段中。 每个帖子都会有这些自定义元字段,月份和年份分开。
解决方法
这种效果:
function hook_save_post($post_id,$post,$update) {
//Check it's not an auto save routine
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can('edit_post',$post_id) )
return;
// Remove this save_post action in case modifications are made in this process
remove_action('save_post','hook_save_post',13);
$post_type = get_post_type($post_id);
switch ($post_type) {
case 'post':
case 'page':
$current_time = (int) current_time('timestamp');
update_post_meta($post->ID,'custom_month',date('m',$current_time));
update_post_meta($post->ID,'custom_year',date('Y',$current_time));
break;
}
// Return save_post action
add_action('save_post',13,3);
}
add_action('save_post',3);