I'm saving my CPT in private
status. So the transition would be from pending
to private
. Thing is, when the post was first submitted and is pending there's a date of that submission on the post_date
field in db. But when the post got published, the date updated with the current date.
I want to keep the original date of the submission of the post even the post privately published later.
So I did something like below:
function mycpt_keep_pending_date_on_publishing( $new_status, $old_status, $post ) {
if( 'mycpt' === $post->post_type && 'pending' === $old_status && 'private' === $new_status ) :
$pending_datetime = get_post_field( 'post_date', $post->ID, 'raw' );
// Update the post
$modified_post = array(
'ID' => $post->ID,
'post_date' => $pending_datetime,
'post_date_gmt' => get_gmt_from_date( $pending_datetime )
);
// Update the post into the database
wp_update_post( $modified_post );
endif;
}
add_action( 'transition_post_status', 'mycpt_keep_pending_date_on_publishing' );
But it's not working. What can be the reason?
{$old_status}_to_{$new_status}
hook. If I were to make a wild guess, I would think that the post_date gets updated on publish before it actually gets set to private.pending_to_publish
andpending_to_private
. Tried updating the post from front end with my code and from admin end with core code. No luck. :(