Skip Navigation

[Resolved] Copy Parent Post Field Value to Child Post Field on CRED New Child Post Save

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 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Author
Posts
#620754

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 );

    }
}
#620799

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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 );
 
    }
}