Skip Navigation

[Resolved] Automatically creating a taxonomy value after user form submission

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

Problem:
The user would like to assign some taxonomy terms, to a post created using a Toolset form, automatically.

Solution:
The code that you shared seems to create a Reg User post. If you want to create a taxonomy term, you will need to use the function wp_insert_term. And if you want to assign the taxonomy term to the post, you can use the wp_set_object_terms function.

To get the value of the field, use the get_post_meta function or you can use the $_POST array data. You may need to add the wpcf- prefix to the field slug.

I hope this helps. Check this ticket for inspiration https://toolset.com/forums/topic/adding-taxonomy-programatically/

Relevant Documentation:

100% of people find this useful.

This support ticket is created 3 years, 11 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by francisN-2 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1865493

Tell us what you are trying to do?
I already have a user form working, which automatically inserts a custom type entry(custom type already exists) and assigns the author(the newly registered user). The custom type "Reg Users" has a taxonomy "Chapter".
What I need is to create a entry for this taxonomy, based on a field "country" which I submit with the form. Thanks for your help. See initial code below. Example : I will like to save "Francis" in the "country" taxonomy for the post type "Reg Users"
Is there any documentation that you are following?

Is there a similar example that we can see?
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// change this 12 to the actual id of your form
if ($form_data['id']==1331)
{
//this is where you will insert your post as described below
// Create post object
$my_post = array(
'post_title' => $_POST['wpcf-first-name'].' '.$_POST['wpcf-last-name'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'reg-users'
);

// Insert the post into the database
wp_insert_post( $my_post );
}
}
What is the link to your site?
still developing site.

#1866267

Hello and thank you for contacting the Toolset support.

The code that you shared seems to create a Reg User post. If you want to create a taxonomy term, you will need to use the function wp_insert_term. And if you want to assign the taxonomy term to the post, you can use the wp_set_object_terms function.
- https://developer.wordpress.org/reference/functions/wp_insert_term/
- https://developer.wordpress.org/reference/functions/wp_set_object_terms/

To get the value of the field, use the get_post_meta function or you can use the $_POST array data. You may need to add the wpcf- prefix to the field slug.
- https://developer.wordpress.org/reference/functions/get_post_meta/

I hope this helps. Check this ticket for inspiration https://toolset.com/forums/topic/adding-taxonomy-programatically/
And let me know if you need further assistance.

#1868559

My issue is resolved now. Thank you!