Skip Navigation

[Resolved] Replace post date to current date based on Types custom field selection

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 6 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#547747

I am trying to: Use a custom checkbox filed for post type "Post" created with Toolset Types to be used to replace the post publish date date to current date and time when updating a post. The checkbox field should be selected by default which will result in replacing the post date and time with the current post date and time when updating post. If unchecked, then the post date should follow the normal WordPress workflow and do not modify the post publish date and time.

I am using this code to accomplish this functionality at the moment:

 function reset_post_date_wpse_121565($data,$postarr) {
  // var_dump($data,$postarr); die; // debug
if (get_post_type() == 'post' OR 'download'){
	$data['post_date'] = $data['post_modified'];
  $data['post_date_gmt'] = $data['post_modified_gmt'];
}
	return $data;
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565',99,2);

I expected to see: Need the above code to be executed only when a checkbox (created with Types custom field) is selected.

Instead, I got: Currently the limitation is that all my posts are being updated, where as I want the logic to be executed only when the checkbox is selected.

#547793

Dear syedao,

I suggest you try with the WordPress built-in action hook save_post, https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
use it to trigger a custom PHP function when user create/edit a post, in this custom PHP function do these:
1) Get the value of custom checkbox field:
https://codex.wordpress.org/Function_Reference/get_post_meta
2) If the value is setup as checked, then replace the post date and time with the current post date and time when updating post
https://developer.wordpress.org/reference/functions/wp_update_post/
3) If the value is setup as unchecked, then do nothing

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.