[Résolu] Custom code works on Dashboard but not when adding posts via CRED Form
Ce fil est résolu. Voici une description du problème et la solution proposée.
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.
This support ticket is created Il y a 3 années et 7 mois. 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.
Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.
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.
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.
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 );
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.