Problem: I have a Form that creates Product posts. Two taxonomies are assigned to Products: Product Categories and standard Categories. When a Product is created by Forms, Users will define the Product Categories. I would like to programmatically assign the same terms in the standard Category taxonomy.
Solution: You can manipulate the terms associated with a new post created by Forms using the Forms API cred_save_data. Add this code to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom Code:
add_action('cred_save_data', 'ts_copy_to_cats_from_pcats',10,2); function ts_copy_to_cats_from_pcats($post_id, $form_data) { $forms = array( 12345 ); if ( in_array( $form_data['id'], $forms ) ) { $post_terms = wp_get_object_terms($post_id, 'product_cat', array('fields' => 'slugs')); wp_set_object_terms($post_id, $post_terms, 'category', false); } }
Replace 12345 with the numeric ID of the New Product Form.
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_get_object_terms
https://codex.wordpress.org/Function_Reference/wp_set_object_terms
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years ago.
Assisted by: Christian Cox.