Skip Navigation

[Resolved] Using a CRED Form to edit the last modified date for a post

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by nicholasF-3 4 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1583069

Hi
I'm trying to get a CRED Form to edit the last modified date of a post or custom post type to a date and time entered in the edit form. I don't know where to start in the cred_dave_data hook given the wp_update_post function will not edit the last modified date. Any help with how I can edit the last modified date from a CRED edit form would be greatly appreciated. Thank you

The following code populates the last modified date successfully in the CRED edit form:
// Populated the edit form with last modified date
//
add_filter( 'cred_filter_field_value_before_add_to_form', 'my_modified_date_function', 10, 2);
function my_modified_date_function($value, $computed_values){
if (isset($_GET['cred-edit-form']) && $_GET['cred-edit-form']==7090)
{
if($computed_values['name'] == 'my-post-modified-date'){
$pid = get_the_ID();
$time = get_the_modified_date('U', $pid);
$value['timestamp'] = $time;
}
}
return $value;
}

I've got similar functionality working with editing the Post Date using the code below which uses the wp_update_post function within the cred_save_data hook:

// Populated the edit form with current post date
//
add_filter( 'cred_filter_field_value_before_add_to_form', 'my_creation_date_function', 10, 2);
function my_creation_date_function($value, $computed_values){
if (isset($_GET['cred-edit-form']) && $_GET['cred-edit-form']==7090)
{
if($computed_values['name'] == 'my-post-creation-date'){
$pid = get_the_ID();
$time = get_the_date('U', $pid);
$value['timestamp'] = $time;
}
}
return $value;
}

// Save the new post date
//
add_action('cred_save_data', 'my_post_creation_date', 100, 3);
function my_post_creation_date($post_id, $form_data) {
if( $form_data['id'] == 7090 ) {
$mysql_time_format= "Y-m-d H:i:s";
$newDate = $_POST['my-post-creation-date']['datetime'] . ' '.$_POST['my-post-creation-date']['hour'].':'.$_POST['my-post-creation-date']['minute'].':00';
$newDate_gmt = gmdate( $mysql_time_format, ( $newDate + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
$my_post = array(
'ID' => $post_id,
'post_date' => $newDate,
'post_date_gmt' => $newDate_gmt
);

// Update the post into the database
wp_update_post( $my_post );
}
}

#1583911

Hi Nicholas,

Thank you for contacting us and I'd be happy to assist.

I've performed some tests on my website and I was able to update the post's modified date, using the code from this thread:
https://stackoverflow.com/a/50366129

Note: this reply also includes information about why "wp_update_post" function can't be used in this case.

Example:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	// if a specific form
	$forms = array( 61 );
	if ( in_array( $form_data['id'], $forms) )
	{
		global $wpdb;

		$time = $_POST['mod-date']['timestamp'];

		$mysql_time_format= "Y-m-d H:i:s";

		$post_modified = gmdate( $mysql_time_format, $time );

		$post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )  );

		$wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}'  WHERE ID = {$post_id}" );
		
	}
}

Note: In this case, my edit form's ID was "61" and the form included a date type generic field with slug "mod-date" to enter a new date value for the modified date.

You're welcome to update this code snippet, as per your requirement.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1585419

Thanks very much Waqar
Are you sure this is editing the last modified date on your test site? When I add your code the last modified date is reverting to the current date and time.

This is what I have in place:
CRED edit form ID 7090

[cred_generic_field type='date' field='fol-post-modified-date']
{
"required":0,
"validate_format":0
}
[/cred_generic_field]

functions.php

add_filter( 'cred_filter_field_value_before_add_to_form', 'fol_get_last_modified_date', 10, 2);
function fol_get_last_modified_date($value, $computed_values){
    if (isset($_GET['cred-edit-form']) && $_GET['cred-edit-form']==7090)
    {
    if($computed_values['name'] == 'fol-post-modified-date'){
        $pid = get_the_ID();
        $time = the_modified_date('U', '', '', $echo = false);
        $value['timestamp'] = $time;
    }
}
    return $value;
}

add_action('cred_save_data', 'fol_edit_last_modified_date',10,2);
function fol_edit_last_modified_date($post_id, $form_data)
{
    // if a specific form
    $forms = array( 7090 );
    if ( in_array( $form_data['id'], $forms) )
    {
        global $wpdb;
        $time = $_POST['fol-post-modified-date']['timestamp'];
        $mysql_time_format= "j F Y H:i:s";
        $post_modified = gmdate( $mysql_time_format, $time );
        $post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )  );
        $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}'  WHERE ID = {$post_id}" );
    }
}

Display the last modified date in template on the front end:

<?php echo the_modified_date(); ?>

The result is always the current date and time no matter what I enter in the date type generic field fol-post-modified-date.

Do you have any thoughts what I might be doing wrong?
Thanks

#1588961

Thanks for writing back and I apologize for the delay in getting back on this.

I've performed some further tests to confirm that the code snippet that I shared earlier is working to update the modified date.

In the code that you've shared the date format at line# 24 is changed to "j F Y H:i:s" while in the snippet that I shared it was "Y-m-d H:i:s" ( line# 13 ).

Since WordPress stores/expects the date/time to be in that specific format ( e.g. 2020-04-13 05:00:00 ) for the modified and created date/time, please make sure that you're using "Y-m-d H:i:s".

#1589987

Thanks Waqar
I've now sorted this - it wasn't because of the date format - it was because when I added your code I created a second CRED_save_data function for the form (I already had the function to save the post created date) rather than merge the two into a single function. The function to save the last edited date was working - but the second function to update the post date was then resetting the last modified date to the curent date/time!

Thank you for helping with this and sorry I didn't spot this when you sent me your working code.

#1589989

My issue is resolved now. Thank you!