Skip Navigation

[Resolved] set value of child post taxonomy form field by the current page post taxonomy

This support ticket is created 4 years, 3 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by Rita 4 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1437795

Hi there

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!

Thanks in advance.

Rita

#1437809

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Rita

According to the documentation for the cred_field shortcode, the value attribute isn't supported for taxonomies: https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field

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.

Would that be an option?

#1437865

Hi Nigel

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.

Do I need to add something to the cred form?

#1437885

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

If you want to give this a go yourself, with the cred_save_data hook

- you have the ID of the newly created child post
- you can use this and the toolset_get_related_post API function to get the ID of the parent (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post)
- or you can probably find that in the global $_POST object which contains the form submission (dump it to your error logs to see how to retrieve the parent post ID)
- use the parent post ID to get the assigned taxonomy terms, using get_the_terms() (https://developer.wordpress.org/reference/functions/get_the_terms/)
- then use wp_set_post_terms to set these terms on the child post

In your sample code you are trying to update post meta, but we are talking about taxonomy terms here, not custom fields.

If you are stuck with the above then let me know and I'll flesh out the example, but you might like to try it first.

#1438091

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?

#1438247

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Rita

I didn't test it, but you should be able to use this, editing the slugs etc as required:

add_action( 'cred_save_data', 'ts_copy_taxonomy', 10, 2 );

function ts_copy_taxonomy( $child_id, $form_data ){

    /** Settings */
    $forms = array( 123 ); // ID(s) of forms
    $tax = 'event-category'; // taxonomy slug
    $relationship = 'event-event-date'; // slug of relationship

    if ( in_array( $form_data['id'], $forms ) ){

        $parent_id = toolset_get_related_post( $child_id, $relationship );
        $parent_terms = get_the_terms( $parent_id, $tax );
        $parent_term_ids = wp_list_pluck( $parent_terms, 'term_id' );
        wp_set_post_terms( $child_id, $parent_term_ids, $tax );
    }
}
#1438457

Ohhh I see... Nigel it works perfectly. Thank you very much indeed.

#1438459

Just closing the thread.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.