Skip Navigation

[Resolved] Match child post taxonomy to parent

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

Problem:
Match child post taxonomy to parent

Solution:
To attach the parent post terms to child posts automatically, I suggest you should use Toolset forms hook cred_save_data.

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/match-child-post-taxonomy-to-parent/#post-1195977

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 5 years, 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by davidS-53 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1195858

I have a Single post template where I have a form to create a child post of a different type

I want to set the same taxonomy terms that the parent has for the child.

I can do this with regular text fields like this:

[cred_field field='make' value='[types field="make"][/types]' force_type='field' class='form-control' output='bootstrap']

Is there an equivalent way to achieve this for taxonomy? Currently I have the below but it doesn't work:

[cred_field field='item' value='[wpv-post-taxonomy type="item"]' force_type='taxonomy' output='bootstrap' display='checkbox']

#1195977

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I see you are using the checkbox field type to display the taxonomy terms. To attach the parent post terms to child posts automatically, I suggest you should use Toolset forms hook cred_save_data.

For example:


add_action('cred_save_data', 'func_set_parent_terms_to_child',20,2);
function func_set_parent_terms_to_child($post_id, $form_data){
 
    // if a specific form
 
    if ($form_data['id']==9999) {
 
	    $parent_post_id = 111;  // adjust your parent post ID here
 
	    $parent_terms = wp_get_object_terms( $parent_post_id, 'item', array('ids') ); 
 

        if(!empty($parent_terms)) {
 
            $parent_term_ids = array();
 
            foreach($parent_terms as $parent_term) {
 
                if($parent_term->term_id != '') {
 
                    $parent_term_ids[] = $parent_term->term_id;
 
                }
            }
 
           if(count($parent_term_ids) > 0) {
 
                wp_set_object_terms( $post_id, $parent_term_ids , 'item' ); 
 
            }
      }
            
    }
}

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://codex.wordpress.org/Function_Reference/wp_set_object_terms

#1196382

Thanks for your suggestion Minesh. Unfortunately the supplied code doesn't work... I've set the form ID correctly, and tried setting $parent_post_id using both $post_id and specifying it manually, and unfortunately the child post does not inherit the parent's "items"... Any idea why that might be?

#1196505

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - it will set automatically, this code will not affect GUI display but it will try to set parent taxonomy terms automatically.

Can you share problem URL and access details?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1198056

OK, it's working now. I realised I still had the GUI for selecting taxonomy terms on my CRED form which must have been overwriting the terms the function was trying to save. Thanks!

#1198057

My issue is resolved now. Thank you!

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