I have a CPT named 'Events' which has a many to many relationship with CPT 'Vessels'. The CPT that connects them is named 'Appearances'.
The CRED Form to create an Appearance simply shows the Parent Event Selector and the Parent Vessel Selector.
When saving the Appearance Post I would like to get the value of a date field in the Selected Parent Event and Copy that value to a date field in the Appearance Post.
Below is the code I'm trying, but it does not appear to work. Lines commented out are other attempts I made to achieve this, for your reference.
Can you advise on what may be the issue?
// New Appearance Form - Save Event Date Timestamp to Appearance Post
add_action('cred_save_data', 'save_event_date_to_timestamp_to_appearance',10,2);
function save_event_date_to_timestamp_to_appearance($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==95) {
// Get ID of Parent Event Selected in CRED Form
$event_id = $_POST['_wpcf_belongs_events_id'];
// Get Date Field value from the post with above ID
// $eventdateto = do_shortcode("[types field='event-date-to' format='U' id='".$_POST["_wpcf_belongs_events_id"]."']");
// $eventdateto = types_render_field( 'event-date-to', array( 'format' => 'U', 'id' => '$event_id' ));
$eventdateto = get_post_meta( $event_id, 'wpcf-event-date-to' );
// Save Parent Date Field value to Appearance Post
add_post_meta( $post_id, 'wpcf-appearance-expiry-date', $eventdateto );
}
}
Hello. Thank you for contacting the Toolset support.
Well - I see you are dealing with event date. I would like to know here in which format the event date is stored?
Toolset uses Unix timestamp to store the date field value. Assuming that date is stored as timestamp, you should try the following code and try to resolve your issue:
// New Appearance Form - Save Event Date Timestamp to Appearance Post
add_action('cred_save_data', 'save_event_date_to_timestamp_to_appearance',10,2);
function save_event_date_to_timestamp_to_appearance($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==95) {
// Get ID of Parent Event Selected in CRED Form
$event_id = $_POST['_wpcf_belongs_events_id'];
// Get Date Field value from the post with above ID
// $eventdateto = do_shortcode("[types field='event-date-to' format='U' id='".$_POST["_wpcf_belongs_events_id"]."']");
// $eventdateto = types_render_field( 'event-date-to', array( 'format' => 'U', 'id' => '$event_id' ));
$eventdateto = get_post_meta( $event_id, 'wpcf-event-date-to' ,true);
// Save Parent Date Field value to Appearance Post
update_post_meta( $post_id, 'wpcf-appearance-expiry-date', $eventdateto );
}
}