Home Forums General Issues Set post publish date by custom field?


Set post publish date by custom field?

  • Hello,
    I have created a data picker field and I would like to set the publication date of my post in wordpress.
    I created this function, which takes the value of my custom field and writes it to the post date.
    Unfortunately, it only works when I update a post and not when I create it, I think the problem is the function:
    wp_update_post how can I fix?
    I would like the action to take place when I create or edit a post.

    // Save ACF custom field to date-time post
    function my_acf_save_post ($post_id) {
         $acfDate = get_field ('data_time_post', $post_id);
         // Test if you receive the data field correctly:
         // echo $ acfDate;
         // exit (-1);
         $my_post = array ();
         $my_post ['ID'] = $post_id;
         $my_post ['post_date'] = $acfDate;
         wp_update_post ($my_post);
    add_action ('acf / save_post', 'my_acf_save_post', 20);

    Thank you all.

  • I don’t see anything wrong with your function except the odd whitespace here

    add_action ('acf / save_post', 'my_acf_save_post', 20);

    if that’s actually what’s in your code I would be surprised if your action runs at all.

  • Hi Jhon, and thanks for your reply.
    The space is an error that I had already solved, in fact the function is correct and run.
    But it only works when I update a post, I would like it to work when I create the post instead.
    Can you help me ?
    Thank you.

  • It should work on either. acf/save_post runs after you create or update a post. I don’t see any reason why it should not be working.

  • Hi, my post is created via formidable form, I don’t know if this can affect.
    For now, however, it only works if I update, when I create the function it does not set the date with the field stored in acf field.

  • Yes, that would make a difference. The ACF hook probably is not run. You will need to use a hook that available in the plugin you are using or in WP that runs after the post is created. Also, you will need to make sure that the ACF field is populated correctly when the post is created.

  • Just adding onto here since this is the code I use on my website;
    Is it possible to address this code to multiple custom posts types?
    E.g. I have custom post types called ‘tv’ and ‘radio’, but I think it wouldn’t be wise to repeat the entire code in the functions.php.

    Sorry if the question seems dumb but I know next to nothing about php.

  • @mfgrijs All ACF filters and actions should do some basic checking, yes you can use the same code for multiple post types.

    add_action('acf/save_post', 'FUNCTION_NAME');
    function FUNCTION_NAME($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'tv' && $post_type != 'radio')) {
        // not your post type
        // do not run
      // .... more code here
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.