Skip Navigation

[Resolved] Custom code works on Dashboard but not when adding posts via CRED Form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to copy a field from the parent post into the child post when submitting a child post form, but the save_post hook is not working.

Solution: Use the cred_submit_complete hook to automatically copy a field from the parent post into the child post when a child post form is submitted. Use the toolset_association_created post relationships API to get the field from the parent post when the relationship is established in wp-admin without forms.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

This support ticket is created 3 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by PaulS4783 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2021819

I have two custom post types, Vehicles and Clients.
When saving a new Vehicle, I have a short code which looks up a Client ID in the client table.
The custom code below then adds the Client ID to the Vehicle record.

This works just fine when creating a post on the Dashboard. But NOT when I save a post in CRED.

function func_update_client_id( $post_id, $post ){
   if ( 'auction-vehicle' == $post->post_type )
    { 
        $client_id =  do_shortcode("[types field='client-id' output='raw' item='@client-username-vehicle.parent'][/types]");
        if ( !empty($client_id)) {
            update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
        }
    }
}
add_action( 'save_post', 'func_update_client_id', 30, 2 );

I understand the post creation flow is a little different in CRED but I'm very confused about how to make this work.

#2022067

Hello, can you try adding this similar code in a cred_submit_complete hook? I am using the post relationships API to get the related Client post, and using the Types render field API to get the custom field value.

// toolset support - 
// copy client ID field from parent client post into vehicle post custom field
// when submitting form for vehicle post
// https://toolset.com/forums/topic/custom-code-works-on-dashboard-but-not-when-adding-posts-via-cred-form/
function func_update_client_id_cred_submit_complete( $post_id, $form_data ){
  // only for this specific form
  if ($form_data['id']==1234)
  {
    // get the parent client post and ID field value
    $client_post = toolset_get_related_post( $post_id, 'client-username-vehicle');
    $client_id = types_render_field('client-id', array('output' => 'raw', 'item' => $client_post));

    // update the vehicle post's custom field value
    if ( !empty($client_id)) {
      update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
    }
  }
}
add_action( 'cred_submit_complete', 'func_update_client_id_cred_submit_complete', 10, 2 );

Replace 1234 with the numeric ID of the Vehicle Form.

References:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/customizing-sites-using-php/functions/

#2022947

Thanks . I will try it.
Just so I understand what's going on:

$client_post = toolset_get_related_post( $post_id, 'client-username-vehicle');
$client_id = types_render_field('client-id', array('output' => 'raw', 'item' => $client_post));

Are " toolset_get_related_post" and "types_render_field" native functions to ToolSet?

#2022949
$client_post = toolset_get_related_post( $post_id, 'client-username-vehicle');
$client_id = types_render_field('client-id', array('output' => 'raw', 'item' => $client_post));
 

Are these "native" ToolSet functions?

#2024989

These are API functions provided by Toolset. For more information, please see the documentation links I provided earlier:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/customizing-sites-using-php/functions/

#2026969

OK. This works for CRED. Thank you.

BUT I need to create a complementary function for when a custom post is added on the Dashboard.
Can I do this with the following code using the "save_post" hook?

function func_update_client_id_save_post( $post_id, $post ){
     if ( 'auction-vehicle' == $post->post_type )  {
             // get the parent client post and ID field value
             $client_post = toolset_get_related_post( $post_id, 'client-username-vehicle');
             $client_id = types_render_field('client-id', array('output' => 'raw', 'item' => $client_post));
 
             // update the vehicle post's custom field value
             if ( !empty($client_id)) {
                update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
             }
     }
}
add_action( 'save_post', 'func_update_client_id_save_post', 10, 2 );
#2027589
gear-icon-menu.png

BUT I need to create a complementary function for when a custom post is added on the Dashboard.
Can I do this with the following code using the "save_post" hook?

Yes...but save_post may not be the most reliable hook for handling this workflow. I suggest a different hook, because post relationship assignment between a Vehicle and a Client is handled separately from publishing the Vehicle and the save_post hook related to that publish event. The relationship may be established later, and save_post is not triggered during that process. Let's say you go to wp-admin > Vehicles > Add New to create a new Vehicle post, and publish it without connecting it to any Client posts in the Post Relationship editor panel. In that case, save_post (for the Vehicle post) is triggered before a Client relationship is created. If you edit the same Vehicle post later and click "Connect Existing Client" to select an existing Client, you will create a relationship between a Vehicle and a Client, but the save_post hook for the Vehicle post is not necessarily triggered during that association process. The Client ID custom field value might not be copied into the Vehicle post's custom field in this scenario.

It probably makes more sense to use the Post Relationship API hook toolset_association_created to trigger some custom code at the time a relationship is established, whether in Forms, in wp-admin, or in custom code:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
That hook should cover both the wp-admin workflow AND the Forms workflow. See the example in the documentation, and notice you will have access to the parent and child post IDs directly as callback arguments. So there's no need for the toolset_get_related_post API in the callback.

Please note there is a potential problem in wp-admin when programmatically setting custom field values in related posts. Basically the value you try to set programmatically will be overwritten with a blank value if you do not refresh the page before saving changes in the related post. If you display the Client ID custom field in the post relationships editor panel of the Vehicle post editor screen, you may experience the problem. For that reason, it is best to disable the client ID field/column in the Vehicle - Client Post Relationship editor panel (see gear-icon-menu.png) from the Vehicle post editor screen. The Client ID field can still be seen when editing the Client post directly, it is only hidden in the Post Relationships editor panel of the Vehicle post editor screen.

We also offer the API toolset_before_association_delete, which you can use to trigger custom code when a Vehicle and a related Client post are disassociated (the post relationship is deleted between two posts). This would be helpful if you want to programmatically delete the Client ID field from the related Vehicle post after disassociation.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete

#2029237

A lot of detail to parse through there.
Thank you for pointing out that feature of Post-relationships (the gear setting).
I will look into that.

You can close this ticket.