Tell us what you are trying to do?
i am trying to capture the cred_association ID of the child post in cred_save_data when the form is submitted.
Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data
Is there a similar example that we can see?
So this works;
add_action('cred_save_data', 'gen_inspection_save',10,2);
function gen_inspection_save($post_id, $form_data)
{
//************************ Generator *************************
if (($form_data['id']==116) || ($form_data['id']==145))
{
$generator_id = $_POST['@generator-gen-inspection_parent'];
$title = Date('d-m-Y', $_POST['wpcf-inspection-date']['datepicker']);
wp_update_post(array('ID' => $post_id, 'post_title' => $title,'post_name' => $title ));
//Update Hours & Last Inspected
update_post_meta( $generator_id, 'wpcf-eng-hours', $_POST['wpcf-eng-hours'] );
update_post_meta( $generator_id, 'wpcf-inspection-date', $_POST['wpcf-inspection-date']['datepicker'] );
}
But i cant grab the child post id from this
if ($form_data['id']==586)
{
$generator_id = $_POST['cred_association_contract-lighting-tower_child'];
//Update status
update_post_meta( $generator_id, 'wpcf-status', 'Hired' );
}
Hello, if this Form creates or edits a child post, then you can use the $post_id variable (the first parameter in the callback arguments) to get the child post ID in a cred_save_data hook:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==586)
{
//Update status
update_post_meta( $post_id, 'wpcf-status', 'Hired' );
}
}
If this Form creates or edits a parent post, then it's not simple because a parent can have many child posts in a one-to-many relationship. If I've misunderstood what you want to accomplish, please provide more details.
Still didn't work. And it doesn't actually put the 'Hired' against any meta in the DB.
if ($form_data['id']==586)
{
//Update status
update_post_meta( $post_id, 'wpcf-status', 'Hired' );
}
i am sure i have the correct form wp-admin/admin.php?page=cred_relationship_form&action=edit&id=586
i do note that the submit for the relationship form is not a button and only a link.
Below is the form data from the XHR header
cred_form_id: 586
cred_form_count: 2
cred_redirect_to: form
cred_ajax_submission: true
cred_redirect_url: hidden link
cred_relationship_slug: contract-lighting-tower
cred_association_form_ajax_submit_nonce: 5d949387e7
_wp_http_referer: /contract/123/?layout_id=588
cred_association_contract-lighting-tower_parent: 607
cred_association_contract-lighting-tower_child: 420
action: cred_association_form_ajax_submit
when i look at the wp_postmeta table for post id 420 the wpcf-status is blank.
Any help would be great!
Thanks
Tim
Actually, it is not running the code. The action is different when submitting a relationship form to that of a CPT edit form.
Does this still get picked up by the cred_save_data hook?
Below is the CPT edit and in the last post is the relationship form.
_cred_cred_wpnonce_cred_form_41: 6c037fbe9d
_cred_cred_prefix_post_id: 436
_cred_cred_prefix_cred_container_id: 436
_cred_cred_prefix_form_id: 41
_cred_cred_prefix_form_count: 1
form_submit: true
action: cred_submit_form
wpnonce: a2aaa61a1c
so i tried cred_submit_complete but that still doesnt appear to fire after the relationship form submitted. It does fire after a CPT edit form has been submitted.
add_action('cred_submit_complete', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
error_log("Oh no! We are out of FOOs!", 1, "me@mycompany.net.au");
}
so i tried cred_submit_complete but that still doesnt appear to fire after the relationship form submitted. It does fire after a CPT edit form has been submitted.
Hello, and sorry for the delay responding to your comments. You have correctly described the source of the problem - Forms API submission events do not apply to relationship forms. APIs and Email Notifications are two features in Post/User Forms that have not yet been implemented for relationship forms. Other users have submitted feature requests for relationship forms APIs, but unfortunately I do not have a delivery date scheduled yet. If you would like to add your input, I encourage you to submit a feature request here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
That will let our management team know the feature is important for you, and you would like to see it prioritized.
Based on the information I have here in the ticket, I think a workaround is possible. You can use the existing post relationship APIs to listen for post association events:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete
These hooks are triggered just after two posts are associated in a post relationship, or just before two posts are disassociated in a post relationship, respectively.
In the callback arguments, you will have access to the post relationship slug, the parent post ID, the child post ID, the intermediary post ID (if it exists), and the unique ID for this association. You would need a conditional to test the relationship slug, so the updates code is only run when associating/disassociating posts in this specific post relationship type. Then you will use get_post_meta and update_post_meta to get or set the date fields, and use wp_update_post to modify the post titles.
Not only will this callback be triggered on the front-end by a relationship form, it will also be triggered on the back-end of the site in wp-admin when you use the Post Relationship panel in the post editor screen. See this ticket for some information about a quirk in a similar situation that requires you to refresh the page in wp-admin after making a change that automatically updates other values in the database. Since the updated values are not instantly reflected in the editor, saving the post would result in overwriting the values that were programmatically changed in the database. In other words, this would effectively revert the changes made by the custom code: https://toolset.com/forums/topic/auto-fill-fields-based-on-related-post/
Let me know your thoughts on this workaround and we can go from there.
My issue is resolved now. Thank you!