Skip Navigation

[Resolved] Add Taxonomy after submission of front end “create new post” form

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

Problem: I have a Form that creates new posts. I would like to automatically add a specific term to the new post using the Forms API, but the update_post_meta function isn't working as expected.

Solution: Try the wp_set_object_terms function with the Forms API cred_save_data:

wp_set_object_terms( $post_id, 'term-slug', 'taxonomy-slug' );

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

100% of people find this useful.

This support ticket is created 5 years, 6 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
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 hui-suanC 5 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1280169

Tell us what you are trying to do?
I want to add the 'Approved' category to the taxonomy (Category) of 'Approved Status' after the "Create event" form has been submitted.

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/

Here is the code which I am using:

add_action('cred_save_data', 'add_event_status',10,2);
function add_event_status($post_id, $form_data)
{

// if the form is create event form, ID: 63
if ($form_data['id']==63)
{
// I am not sure what function to use but I want to add the 'Approved' category to the taxonomy of 'Approved Status'
update_post_meta($post_id, 'wpcf-approved-status', 'approved');
// add_post_meta($post_id, 'wpcf-approved-status', $_POST['wpcf-approved-status'], true);
}
}

#1280353

Hi, check the WordPress API function wp_set_object_terms:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

You need to know the object ID (which is $post_id in your example), the term slug or ID, and the taxonomy slug. Then the format is like this:

wp_set_object_terms( $post_id, 'term-slug', 'taxonomy-slug' );
#1284759

My issue is resolved now. Thank you!