Dynamically fill a post custom field with post publish date


I created a custom field using Advanced Custom Fields (but I think ACF is optionnal) and I want him to dynamically fill the publish post date when I publish a post.

Example :
If I publish a post today, I want when the action of publishing is done to find 30/09/2020 in my custom field
If I schedule my post for tomorrow then I want to find tomorrow 01/10/2020 in my custom field.
Same idea if I publish my article at an earlier date.

I already tried this (in relation with ACF plugin but any other type of solution is find to me) :

add_action('acf/save_post', 'my_acf_save_post');
function my_acf_save_post( $post_id ) { 
    $value = get_the_date();
    update_field('name_of_my_custom_field', $value, $post_id);

But I always get today’s date and not the real published date.

One other thing, I don’t want my custom field to be update when I will update my post in the future. So I thought about a condition like : IF my custom field is fill with a data THEN i do nothing but I am not able to do it.

Any ideas ?

Thanks by advance and sorry for my poor english.

Jonathan Aryup 4 months 0 Answers 12 views 0

Leave an answer