I would like to be able to set the value of a child post's custom taxonomy in an embedded cred form by getting the taxonomy of the parent post where the form is displayed.
Example..
Within a parent EVENT content template I have displayed a form to create a child EVENT-DATE. The EVENT post and the EVENT-DATE post share several custom fields and taxonomies so I would like to preset the value of some custom fields and taxonomies in the child form so that the user doesn't have to manually enter this data each time they create an EVENT-DATE child post.
This works fine for any 'normal' custom field:
[cred_field field='address-one' force_type='field' class='form-control' output='bootstrap' value="[types field='address-one'][/types]"]
But how do I get the value for the custom taxonomy?
[cred_field field='event-category' force_type='taxonomy' output='bootstrap' display='checkbox' value='?????????']
I can't seem to find any applicable forum topics or documentation on this... That I can understand that is. Hoping someone can help me!
So, it's not possible to preselect a taxonomy field.
Does your form need the taxonomy selector, so that it could be changed if the user wanted to?
Because if the requirement is simply that the child post has the same taxonomy terms assigned as the parent post then you could do that via the cred_save_data hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data), where you add a PHP snippet that gets the terms assigned to the parent post and assigns the same to the newly created child post.
Hope you are well. Oh yes, that would fine! The user doesn't need to see or use the selector.
So I tried to do the #cred_save_data hook as you suggested. I added this to the functions.php file:
add_action('cred_save_data', 'copy_event_taxonomy',10,2);
function copy_event_taxonomy($post_id, $form_data) {
if ($form_data['id']==59590) {
if (isset($_POST['event-category'])) {
add_post_meta($post_id, 'event-category', $_POST['event-category'], true);
}
}
}
The '59590' is the ID of the child post form and 'event-category' is the slug of the taxonomy for both the event parent and event-date child. Is this right? Because... nothing happens when I created an event date as a test. The taxonomy is not copied across.
I gave it a go... I read all the documentation. I tried to understand. I won't even grace the page with my efforts. None of which worked of course. I should have mentioned that I don't know php at all... (something I would like to learn properly) Would it be ok if you showed me and just gave me a little explanation for each bit - get the parent id, get the parent taxonomy array, cred_save_data hook for specified post form... which are the three things yes?