繁体   English   中英

Wordpress:将发布的月份和年份导出到自定义元字段中

[英]Wordpress: Exporting month and year of post into custom meta field

我想将常规 Wordpress 帖子的发布日期保存到自定义元字段中。 有没有办法根据下面的场景编写 function ?

需求场景:当我保存/创建帖子时,一些 function 将占用当前月份和年份,然后将这些数据保存到名为 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', 'hook_save_post', 13, 3);
}

add_action('save_post', 'hook_save_post', 13, 3);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM