Support

Account

Home Forums Front-end Issues How do the date of publication by a front-end form?

Solved

How do the date of publication by a front-end form?

  • Hello!

    I ask for help with a great plugin. I’m creating a frontend form for posting. I can enter the name of the post, the content of the post, the post taxonomy, the post format&status, but can not choose the date of publication. I have found no information on the matter. Is that possible?

  • Hi @buylov

    You can only set the date of publication manually using wp_insert_post parameters. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/acf_form/.

    If you want, you can create a dummy date field and use acf/pre_save_post to change the date.

    I hope this makes sense.

  • At the moment I’m using the following code:

            acf_form(array(
                     'post_id' => 'new_post',
                                            'post_title' => true,
                                            'post_content' => true,
                     'new_post'    => array(
                         'post_type' => 'post',
                         'post_status' => 'publish'
                     ),
           'return'     => '%post_url%',
           'submit_value'  => 'Опубликовать'
                  ));

    Please give an example of code that runs fast selection of the date of publication.

  • Hi @buylov

    Maybe you can use something like this:

    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
    
        }
        
        $posted_date = $_POST['acf']['field_1234567890'];
        $theDate = date_create_from_format('Ymd', $posted_date);
        $theDate = date_format($theDate, 'Y-m-d H:i:s');
        
        // Create a new post
        $args = array(
            'post_status'  => 'publish' ,
            'post_type'  => 'post' ,
            'post_date' => $theDate ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $args ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    where field_1234567890 is your date field key. Keep in mind that I haven’t tested it yet.

    Hope this helps.

  • Hmmm … frontend-form no longer appear on my site. I do not fully understand: this code is not used function acf_form(); ?

  • Hi @buylov

    You still need the acf_form() in the templates and this code in the functions.php file.

    Also, you need to pass the post title and post content. It should be something like this:

    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
    
        }
        
        $posted_date = $_POST['acf']['field_1234567890'];
        $theDate = date_create_from_format('Ymd', $posted_date);
        $theDate = date_format($theDate, 'Y-m-d H:i:s');
        
        // Create a new post
        $args = array(
            'post_status'  => 'publish' ,
            'post_type'  => 'post' ,
            'post_date' => $theDate ,
            'post_title' => $_POST['acf']['_post_title'] ,
            'post_content' => $_POST['acf']['_post_content'] ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $args ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    I hope this helps.

  • Thanks for the help! I do not understand why your code calls a server 500 error.

    1

  • Hi @buylov

    You need to put it inside functions.php file (at the bottom part), not page.php. Please take a look at this page: https://codex.wordpress.org/Functions_File_Explained.

  • The code is missing a semicolon.

    // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
    
        }

    It should be as follows:

    // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id;
    
        }

    But I was not able to achieve success with your code. I use the Date Picker field with default settings. It is right?

  • Hi @buylov

    I’m sorry about that. I guess I missed that semicolon.

    Yeah, it should be OK with the default settings. Have you changed the “field_1234567890” with the field key of your date field?

    If you have, could you please attach the JSON or XML export of your field group, the functions.php and the page.php?

  • Semicolon is missing on the documentation page: http://www.advancedcustomfields.com/resources/acf-pre_save_post.

    Yes, I changed the key date field.

    Attach files:

    Export field

    Functions.php

    Page.php

  • Hi @buylov

    It should be working. I don’t know why the new post is not detected. As a workaround, you can use acf/save_post instead. Something like this:

    function my_acf_save_post( $post_id ) {
        
        // get new value
        $theDate = get_field('post-date', $post_id, false);
        $theDate = date_create_from_format('Ymd', $theDate);
        $theDate = date_format($theDate, 'Y-m-d H:i:s');
        
        $my_post = array(
          'ID'           => $post_id,
          'post_date'   => $theDate,
        );
        // do something
        
        // unhook this function so it doesn't loop infinitely
        remove_action('acf/save_post', 'my_acf_save_post');
    
        // update the post, which calls save_post again
        wp_update_post( $my_post );
    
        // re-hook this function
        add_action('acf/save_post', 'my_acf_save_post');
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    I hope this does the trick!

  • It works, thank you very much! But there are two notes, could you help?

    1) If you edit the date field via the backend ACF form, post is not updated. An error 504 Gateway Time-out. At the same time, if you create or update post the frontend ACF form shape works well.

    2) All times are not the same as the one used on the site.

  • Hi @buylov

    That is the disadvantage of using wp_update_post() in “acf/save_post”. If you’re sure that this function is always working on the front end, you can check if the post is posted from the front end or not. It should be something like this:

    function my_acf_save_post( $post_id ) {
        
        if(!is_admin()){
        
            // get new value
            $theDate = get_field('post-date', $post_id, false);
            if($theDate){
                $theDate = date_create_from_format('Ymd', $theDate);
                $theDate = date_format($theDate, 'Y-m-d H:i:s');
                
                $my_post = array(
                  'ID'           => $post_id,
                  'post_date'   => $theDate,
                );
                // do something
                
                // unhook this function so it doesn't loop infinitely
                remove_action('acf/save_post', 'my_acf_save_post');
    
                // update the post, which calls save_post again
                wp_update_post( $my_post );
    
                // re-hook this function
                add_action('acf/save_post', 'my_acf_save_post');
            }
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Yeah, it’s a little bit ugly, but I can’t think anything else for now.

    I’m not sure about your second question. Could you please attach some screenshots?

  • 1) Can I hide the date field in the form of backend? But keep in frontend form.

    2) When editing the date fields does not match the time zone.

    1

    2

  • Hi @buylov

    You can add a style to hide this field using the “acf/admin_head” hook. Something like this I think:

    function my_acf_admin_head() {
    	?>
    	<style type="text/css">
    
    		.post-php .acf-field-5694a485a4a99, .post-new-php .acf-field-5694a485a4a99 {display:none;}
    
    	</style>
    	<?php
    }
    
    add_action('acf/input/admin_head', 'my_acf_admin_head');

    To make it use your site’s timezone, kindly change “post_date” to “post_date_gmt”. This page should give you more idea about it: https://codex.wordpress.org/Function_Reference/wp_insert_post.

  • Thank you, I successfully hid the Date field.

    On the second issue to no avail. I changed:

    'post_date' => $theDate,

    On this:

    'post_date_gmt' => $theDate,

    But in this case, the code is not work. What can be wrong?

  • Hi @buylov

    That’s weird. I tested it on my end last time and it’s working. Could you please explain to me what do you mean by “not work”? Does the date change to the selected date or use the current date?

    If you want to set the time manually, I think you can add two more dummy fields for the hours and minutes. After that, you can add it to $theDate variable like this:

    $theDate = get_field('post-date', $post_id, false);
    $theDate = $theDate . "|" . get_field('dummy_hour', $post_id, false);
    $theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
    if($theDate){
        $theDate = date_create_from_format('Ymd|H:i', $theDate);

    I hope this helps.

  • Strangely enough, but the code does not work either. After the publication sets the current date and time. I use:

    function my_acf_save_post( $post_id ) {
        
        // get new value
    $theDate = get_field('post-date', $post_id, false);
    $theDate = $theDate . "|" . get_field('dummy_hour', $post_id, false);
    $theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
    if($theDate){
        $theDate = date_create_from_format('Ymd|H:i', $theDate);}
    
        $my_post = array(
          'ID'           => $post_id,
          'post_date'   => $theDate,
        );
        // do something
        
        // unhook this function so it doesn't loop infinitely
        remove_action('acf/save_post', 'my_acf_save_post');
     
        // update the post, which calls save_post again
        wp_update_post( $my_post );
     
        // re-hook this function
        add_action('acf/save_post', 'my_acf_save_post');
        
    }
     
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    At the same time, this code works: the date varies.

    function my_acf_save_post( $post_id ) {
        
        if(!is_admin()){
        
            // get new value
            $theDate = get_field('post-date', $post_id, false);
            if($theDate){
                $theDate = date_create_from_format('Ymd', $theDate);
                $theDate = date_format($theDate, 'Y-m-d H:i:s');
                
                $my_post = array(
                  'ID'           => $post_id,
                  'post_date'   => $theDate,
                );
                // do something
                
                // unhook this function so it doesn't loop infinitely
                remove_action('acf/save_post', 'my_acf_save_post');
    
                // update the post, which calls save_post again
                wp_update_post( $my_post );
    
                // re-hook this function
                add_action('acf/save_post', 'my_acf_save_post');
            }
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • Hi @buylov

    My bad. It should be like this:

    function my_acf_save_post( $post_id ) {
        
        // get new value
        $theDate = get_field('post-date', $post_id, false);
        $theDate = $theDate . " " . get_field('dummy_hour', $post_id, false);
        $theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
        if($theDate){
            $theDate = date_create_from_format('Ymd H:i', $theDate);
            $theDate = date_format($theDate, 'Y-m-d H:i:s');
    
            $my_post = array(
              'ID'          => $post_id,
              'post_date'   => $theDate,
            );
            // do something
            
            // unhook this function so it doesn't loop infinitely
            remove_action('acf/save_post', 'my_acf_save_post');
         
            // update the post, which calls save_post again
            wp_update_post( $my_post );
         
            // re-hook this function
            add_action('acf/save_post', 'my_acf_save_post');
        }
        
    }
     
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Don’t forget to create the dummy hours and minutes fields (you can use number field for this).

    I hope this one is working 🙂

  • Thank you, it works. But number fields is not very convenient. There are ACF addon: Date and Time Picker Field. It displays the date and time field. It may be possible to modify this part of the code for it:

     // get new value
        $theDate = get_field('post-date', $post_id, false);
        $theDate = $theDate . " " . get_field('dummy_hour', $post_id, false);
        $theDate = $theDate . ":" . get_field('dummy_minute', $post_id, false);
        if($theDate){
            $theDate = date_create_from_format('Ymd H:i', $theDate);
            $theDate = date_format($theDate, 'Y-m-d H:i:s');
    
            $my_post = array(
              'ID'          => $post_id,
              'post_date'   => $theDate,
            );

    With this plugin, you can use only one field. The plugin can display the date and time in any format:

    1

    2

    Date and time field appears good.

    3

    You can modify the code for one field? And say what format the date and time settings to use this code?

  • Hi @buylov

    Nice finding! I forgot about this plugin. With this plugin, it should be easier. I think something like this will work:

    function my_acf_save_post( $post_id ) {
        
        if(!is_admin()){
            // get new value
            $theDate = get_field('date_time', $post_id, false);
            
            if($theDate){
    
                $my_post = array(
                  'ID'          => $post_id,
                  'post_date'   => $theDate,
                );
                // do something
                
                // unhook this function so it doesn't loop infinitely
                remove_action('acf/save_post', 'my_acf_save_post');
             
                // update the post, which calls save_post again
                wp_update_post( $my_post );
             
                // re-hook this function
                add_action('acf/save_post', 'my_acf_save_post');
            }
        }
    }
     
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Please change the “date_time” with your date time field name. Also, you need to set the date format to yy-mm-dd and time format to HH:mm:ss.

    I hope this helps.

  • James, I thank you for your help. It was a long topic, but I hope it will be useful to the user in the creation of ACF front-end forms.

  • OK – this looks like what I’m looking for – I have an acf form with field (date-time) date/time picker that I would like to use to set the publication date of the draft post

    The acf form works fine in my wp template – I just need the date/time picker to set the publication date…?

    Could you please highlight what I need to do with the above to get the basics working?

    I have tried putting this code into my functions.php without any luck…

    function my_acf_save_post( $post_id ) {
        
        if(!is_admin()){
        
            // get new value
            $theDate = get_field('post-date', $post_id, false);
            if($theDate){
                $theDate = date_create_from_format('Ymd', $theDate);
                $theDate = date_format($theDate, 'Y-m-d H:i:s');
                
                $my_post = array(
                  'ID'           => $post_id,
                  'post_date'   => $theDate,
                );
                // do something
                
                // unhook this function so it doesn't loop infinitely
                remove_action('acf/save_post', 'my_acf_save_post');
    
                // update the post, which calls save_post again
                wp_update_post( $my_post );
    
                // re-hook this function
                add_action('acf/save_post', 'my_acf_save_post');
            }
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
     
  • This reply has been marked as private.
Viewing 25 posts - 1 through 25 (of 26 total)

You must be logged in to reply to this topic.