Support

Account

Home Forums Front-end Issues Updating post modified date

Solved

Updating post modified date

  • We use ACF for a front-end form to create and edit posts (using a custom post type). These posts are edited and updated on a regular basis.

    On the post display we use the_modified_date() to display the date the post was last modified. If we edit the posts in the wp-admin, this date is updated. If we edit the posts in the ACF front-end form the date is not updated.

    We would like to update the modified date anytime a front-end post is edited. Here is our code for the front end form (for an existing post). Must be simple but I can’t figure it out. Any ideas?

    $post = get_post( $id );
    
    $args = array(
       'post_id'          => $id,
       'post_status'      => $post->post_status,
       'submit_value'  => 'Update Report',
            'return'   => get_permalink( $id ) . '?updated=true',
        );
    
    acf_form( $args );
  • Hi @dano23

    I’m afraid you need to use the acf/save_post hook and then update the modified date manually using the wpdb class. Please take a look at this thread to learn more about it: http://stackoverflow.com/questions/27308459/wordpress-change-last-modified-date-of-the-post-to-post-date-scheduled-post.

    Hope this helps.

  • Ok, thanks. I think I was able to get this working. The post_modified date is now updating when we edit on the front end. However, I am also seeing new errors pop up with another plugin, so not sure if I did this right. (It’s possible these errors may not be related.)

    I’m not that familiar with the wpdb class so let me know if anything looks incorrect.

    Here is my code:

    function my_acf_save_post( $post_id ) {
    
      // bail out early if we don't need to update the date
      if( is_admin() || $post_id == 'new' ) {
    
         return;
    
       }
    
       global $wpdb;
    
       $datetime = date("Y-m-d H:i:s");
    
       $query = "UPDATE $wpdb->posts
    	     SET
                  post_modified = '$datetime'
                 WHERE
                  ID = '$post_id'";
    
        $wpdb->query( $query );
    
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    Thanks!

  • Hi @dano23

    I think it looks great. Could you please check if the error still shows up even when you disabled that function?

    Thanks 🙂

  • Everything seems to work fine now. It seems the changes I implemented above cause some errors with another plugin that were not cropping up until we added these. They’re fixed now, and that plugin was custom database development so I don’t imagine it affecting anyone else.

    Thanks for the help — things are working great now and all of our reports are showing the “last updated” date.

  • Thanks. I needed this as well !!

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

The topic ‘Updating post modified date’ is closed to new replies.