admin管理员组文章数量:1130349
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 badges2 Answers
Reset to default 2Using 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 );
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 badges2 Answers
Reset to default 2Using 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 );
本文标签: How to automatic update date and time when save custom post type
版权声明:本文标题:How to automatic update date and time when save custom post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749126894a2319933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论