Home › Forums › Front-end Issues › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.