如何解决Wordpress:将帖子的月份和年份导出到自定义元字段中
我想将常规 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。