Skip Navigation

[Resolved] Change parent post's status/field when child post saved

This support ticket is created 7 years 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.

Our next available supporter will start replying to tickets in about 5.04 hours from now. Thank you for your understanding.

This topic contains 2 replies, has 1 voice.

Last updated by melS 7 years ago.

Author
Posts
#589200

Tell us what you are trying to do?
I have a parent post type, Show, which has a child post type, Performance.
These are set up as 2 types with parent-child relationship as one show can have multiple performances at various venues and times.

As every show must have at least one time&place (performance) before it can be published. I'd like the "save" of the main event to be shown as incomplete in some way (not necessarily using the built in draft/pending/published, as I think that may interfere with the workings of the child forms) and then once a child post has been added, the parent post should now be "live"

I need to implement this using CRED forms (i.e. this content is being created using the wordpress backend)

I have a "create show" page that shows the initial parent form, which redirects to a second "create step 2" page that uses a view to query for the parent ID and display the parent details again along with a create form for the child content. It is on the "save" of this page that the parent show should be live on the front end.

If I save the parent post as a draft the second form does not load correctly as the view only returns published posts.

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/cred-forms-for-child-content/
https://toolset.com/documentation/beyond-the-basics/post-relationship-concepts/implementing-one-to-many-relationships/

#589203

Leaving this thread open for now but I think this other thread may contain the answer I need https://toolset.com/forums/topic/display-taxonomy-associated-with-port-parent/#post-570754

#589241

That thread and this other (https://toolset.com/forums/topic/cred-add-child-content-and-update-parent-attribute/) helped me work out a function to do as I needed.

My page loads a view that gets the event (the "show" mentioned earlier) to be edited by url parameter. The view then loads an edit form within the loop for the event (saved separately on a different tab) and a create form for the child (performance) which calls this function:

add_action('cred_save_data_108', 'update_parent_event',10,2);
function update_parent_event($post_id, $form_data)
{
    write_log("update_parent_event start");
    write_log($form_data);
    $form_id = $form_data['id'];
    $performance_id = $post_id;
    $parent_event_id = $form_data['container_id'];

    write_log( "performance_id: {$performance_id}, form_id: {$form_id}, parent_event_id: {$parent_event_id}" );

    if($parent_event_id) {
        write_log("updating performance parent id");
        update_post_meta($performance_id, '_wpcf_belongs_event_id', $parent_event_id);
        write_log("updating parent status to live");
        update_post_meta($parent_event_id, 'wpcf-status', 'live');
    }
    write_log("update_parent_event end");
}

(for writing to logs see here hidden link )