$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to automatic update date and time when save custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to automatic update date and time when save custom post type

matteradmin8PV0评论

I need to update date and time when I save posts. I see the code on the following thread, but I need to have this trick only for posts contained in my custom post 'new_cpt'

Is it possible?

ref: Update post date to modified date automatically

I need to update date and time when I save posts. I see the code on the following thread, but I need to have this trick only for posts contained in my custom post 'new_cpt'

Is it possible?

ref: Update post date to modified date automatically

Share Improve this question edited Dec 5, 2018 at 13:49 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 5, 2018 at 13:22 GiulioGiulio 511 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Using your example link, I just added an IF to check for post_type.

function reset_post_date_wpse_121565($data,$postarr) {
    // var_dump($data,$postarr);  die;// debug
    if($data['post_type'] == 'new_cpt'){
        $data['post_date'] = $data['post_modified'];
        $data['post_date_gmt'] = $data['post_modified_gmt'];
        return $data;
    }    
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565',99,2);

Yes, it is possible. All you have to do is to check the post type before you update dates:

function reset_post_date_wpse_321084( $data, $postarr ) {
    if ( array_key_exists('ID', $postarr) ) {
        $post_id = $postarr['ID'];

        if ( 'new_cpt' == get_post_type( $post_id ) ) {
            $data['post_date'] = $data['post_modified'];
            $data['post_date_gmt'] = $data['post_modified_gmt'];
        }
    }

    return $data;
}
add_filter( 'wp_insert_post_data','reset_post_date_wpse_321084', 99, 2 );
Post a comment

comment list (0)

  1. No comments so far