Support

Account

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

Solved

Set post publish date by custom field?

  • 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!

  • 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

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

  • 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

  • @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?

  • Thanks for your help.
    However, I would like the function to be activated both when the post is created and when it is updated …
    What could I use instead of wp_update_post?

    Thanks in advance.

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

The topic ‘Set post publish date by custom field?’ is closed to new replies.