Support

Account

Home Forums Backend Issues (wp-admin) Set post publish date by custom field?

Solved

Set post publish date by custom field?

    • davelee

    • March 31, 2014 at 8:17 pm

    Hi there, I create Calendar plugin based on ACF.
    The begin/end Date are ACF-Fields.
    Now my question: It is possible to set the “publish date” with the ACF_begin_date?

    I read this http://www.advancedcustomfields.com/resources/actions/acfsave_post/
    but I don’t know how I can manipulate the publish date.

  • I found the solution with wp_update_post().

    
    add_action('save_post', 'change_content');
        global $post;
        global $wpdb;
        function change_content($post_id) {
    	$datefield = get_post_meta($post_id,'acf-event-date',true);
    	$post_date = date("Y-m-d H:i:s", strtotime($datefield));
    	$my_post = array();
    	$my_post['ID'] = $post_id;
    	$my_post['post_date'] = $post_date;
    	
    	remove_action('save_post', 'change_content');
            wp_update_post( $my_post );
    	add_action('save_post', 'change_content');
    }
    	
    

    Davelee

  • Man , thanks a LOTTT !!!

  • You, sir, are the man!

    Thank you!

    • iproject

    • September 23, 2016 at 5:05 pm

    Hi! This done the job for me :
    Paste this function on your functions.php theme.

    // Save ACF custom field to date-time post
    function my_acf_save_post( $post_id ) {
        $acfDate = get_field('initial_time_var', $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);

    Notes: Be sure to give the correct post id variable ($post_id) and the correct time variable (initial_time_var for me)

    Important: Set the field type (data picker) in your ACF panel to return this custom exact format: Y-m-d H:i:s

    • ma2n13

    • September 29, 2017 at 11:06 pm

    how to see post id variable ($post_id) in my site?

    • pjsando

    • January 4, 2018 at 1:31 am

    Hi,
    I’m having a problem implementing this code and could do with a hand.
    I have an ACF date/time selector field ‘post_date’ that I want to create publication date in new post
    I have set date/time format to Y-m-d H:i:s
    And I have put the following code into my functions.php

    function my_acf_save_post( $post_id ) {
    	$acfDate = get_field('post_date', $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);

    I have not set $post_id – is this the reason it is not working do you think? If so, how do i set post_id?

    Thanks for your help

    • Rasso

    • January 28, 2018 at 11:10 pm

    @pjsando this only works, if you get the date field unfiltered, directly from the database:

    
    $acfDate = get_field('post_date', $post_id, false); // the last argument, 'false', gets the field unfiltered from the db.
    
  • This code is changing the date on on every page on my site, any idea why? Everything (except the custom post type w/ the Birthday field) is published on January 1, 1970. Any help on making it so it only works on post types sp_player?

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.