Support

Account

Home Forums General Issues Use Date/Time picker as post publication date

Solving

Use Date/Time picker as post publication date

  • Hello,

    I’m trying to use a date/time picker field to define the publication date of a draft post.

    I’ve seen one or two other discussions on a similar subject but not had any success implementing the associated code.

    I’m wondering if anyone has any ideas…?

    Thanks

  • Do you have some references for this other code? Are you trying to do this on the front end or in the admin? Please supply more information.

  • Hi John
    Thanks for your reply.

    There were two posts I found on acf about setting publication post date using acf date/time picker on the fontend. Another post I’ve found on stackexchange.

    https://support.advancedcustomfields.com/forums/topic/set-post-publish-date-by-custom-field/

    https://support.advancedcustomfields.com/forums/topic/how-do-the-date-of-publication-by-a-front-end-form/

    https://wordpress.stackexchange.com/questions/219036/acf-field-to-set-publish-date-post-duplication-upon-update

    In any case – I have an acf form on the front end where the user creates a draft post. The form works fine and creates the draft post as required. However, I’d like to be able to include a date/time picker and have the selected date/time set as the post’s publication date. I tried using the following code but had no luck

    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);

    Any ideas?
    Thanks
    P

  • Since it is being saved as draft, do you have a pre_save_post filter that is doing this? If so, the I would change the post date there. Otherwise, you should do some testing here to make sure that you’re updating the right post since this will run on every post that’s saved.

    More than likely the reason the post date is not being updated is that the value returned by ACF is not a valid date that can be inserted. What is the return format for the date field. It should be ‘Y-m-d H:i:s’. I don’t see anything else in your code that would cause the date not to be updated. You can check this by

    
    $post = wp_update_post( $my_post );
    if (is_wp_error($post)) {
      echo 'WP ERROR: <pre>'; print_r($wp_error); echo '</pre>'; die;
    }
    
  • I have an acf form page with the following code

    <?php
    /*
    Template Name: Featured
    */
    acf_form_head();
    get_header();?>
    
    <div id="container" class="row">
            <div id="primary" class="small-12 columns">
    		
              	<?php
    					 acf_form(array(
    						 'post_id' => 'new_post',
    						 'field_groups' => array(54), // Used ID of the field groups here.
    						 'post_title' => true, // This will show the title filed
    						 'post_content' => true, // This will show the content field
    						 'form' => true,
    						 'new_post' => array(
    							
    							 'post_status' => 'draft' // You may use other post statuses like draft, private etc.
    						 ),
    						 'return' => '%post_url%',
    						 'submit_value' => 'Review',
    					 ));
    			
    			?>
    
            </div><!-- #primary -->           
    	</div> <!-- #container -->
        
        <?php get_footer(); ?>
    

    This takes the user to a review page where the they can edit further if they wish.

    Can I then update the publication date within the code above?

    Also, the code for error testing – where would I put that?

    Thanks
    Patrick

  • to update the date you need to write a filter. This can be done either on the acf/save_post filter like you are trying or https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    I would stick with what you’re trying. I really think that the date is not being updated because of a badly formatted date, see my last comment.

    error checking can only be done in an ACF form by creating acf/validate_value filters https://www.advancedcustomfields.com/resources/acf-validate_value/

  • OK – the return format for the date field is definitely correct – ‘Y-m-d H:i:s’

    I have tried changing post_date to field_5a4cd9975f9a7 – no luck.

    Post Publish details still show ‘Immediately’ with current date/time.

    Maybe the error checking code will help me, but I am still struggling a bit to understand where to place it – should I place it in my frontend page with acf_form – if so where in the page should it go?
    Or should it be placed in the functions.php – if so where?

    Thanks for your help
    P

  • Put it in your function that is updating the post date.
    Instead of

    
    wp_update_post( $my_post );
    

    use

    
    $post = wp_update_post( $my_post );
    if (is_wp_error($post)) {
      echo 'WP ERROR: <pre>'; print_r($wp_error); echo '</pre>'; die;
    }
    

    This should cause the error to be displayed it there is an error.

  • Thanks again. Done that

    function my_acf_save_post( $post_id ) {
        global $wp_query;
        $post_id = $wp_query->get_queried_object_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;
        $post = wp_update_post( $my_post );
    		if (is_wp_error($post)) {
    		  echo 'WP ERROR: <pre>'; print_r($wp_error); echo '</pre>'; die;
    		}
    
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    But there seems to be no change in the behaviour of the acf-form – the post is still saved as draft – the custom date field is completed but the post’s publish details still say ‘Publish Immediately’ with today’s date…

  • Ah, of course that date will not be copied over to the posts’s publish date because I’ve just taken out that piece of code and replaced it with the error checking code – sorry

  • Can you look in the database and check the saved publish date to see if it’s actually being updated?

  • Easy if you know how! I tried vie myphp but couldn’t see saved publish date as a field.

    My feeling is that $acfDate is not being populated – can I echo for test?

  • OK – with the below code $post_id is echoed as ‘8’ – which is NOT the id number of the draft post (when i look at the link in admin), and $acfDate echoes nothing, I guess because the correct post id is not being obtained

    function my_acf_save_post( $post_id ) {
        global $post;
        $post_id = $post->ID;
    	$acfDate = get_field('field_5a4cd9975f9a7', $post_id);
        //Test if you receive the data field correctly:
        echo $post_id;
    	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);
    
  • Sorry for not getting back to you sooner, I’ve been meaning to test this but I just haven’t had the time.

    Finding a post in phpMyAdmin
    Open up the table wp_posts, or {WHATEVER YOUR PREFIX IS}._post. Go to search. Enter the post ID that you’re looking for. find the column publish_date

  • OK – so it seems that the post_date is being set to current, but post_date_gmt is not being set at all – please see attached

  • Better pic

  • I couldn’t find a publish_date – just post_date and post_date_gmt

  • I tested this and it seems to be working for me as long as the return date is in the correct format.

    I would debug by outputting values to see what’s going on. Make sure that the function is actually called, make sure the date you’re trying to set is in the correct format, etc. There does not really appear to be any reason it wouldn’t work except the date format.

  • Hi John,
    would you be willing to post your code here for reference?

    Thanks.

  • this works for me

    
    add_action('acf/save_post', 'test_update_post_date_from_acf', 20);
    function test_update_post_date_from_acf($post_id) {
    	// remove this filter to prevent potential infinite loop
    	remove_filter('acf/save_post', 'test_update_post_date_from_acf', 20);
    	// date format must be "Y-m-d H:i:s"
    	$post_date = get_field('acf_post_date_time_field');
    	$post = wp_update_post(array(
    		'ID' => $post_id,
    		'post_date' => $post_date));
    }
    
  • John, I know this thread has been dead for a bit but I have a question about the code snippet above- is there any way to force the post_status to future? I tried adding

    'post_status' => 'future',

    to the wp_update_post array but it still publishes the post on the date specified in the ACF field. I have been trying to figure out how to schedule a post with ACF for two weeks now and this is the closest I have gotten. Any help would be appreciated.

  • See this topic, it seems that in order to set the post status to future you also need to set post_date_gtm, https://wordpress.stackexchange.com/questions/67451/wp-update-post-doesnt-update-post-status

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

You must be logged in to reply to this topic.