I've created a custom post type and taxonomy using Custom Post Type UI plugin. There is a form on the frontend that when filled out creates a new post in the custom post type Survey. I'd like for the title of the post (this is dynamically added based on another post) to be the taxonomy term, custom taxonomy is survey_category. I found this:
add_action('publish_survey', 'add_survey_term');
function add_survey_term($post_ID) {
global $post;
$survey_post_name = $post->post_name;
wp_insert_term( $survey_post_name, 'survey_category');
}
but it doesn't seem to work. I need it to insert the term and if the term already exists I need it to update the number of posts associated with that term.
You can use wp_set_object_terms()
to add taxonomy to your post dynamically. This function will insert the term and if the term already exists, it will update the number of posts associated with that term. I have done small example with post name & taxonomy name you can try below code:
$name = $_POST['value']['post_name'];
$tax_name = $_POST['value']['tax_name'];
$args = array(
'post_title' => $name,
'post_type' => 'your_post_type',
'post_status' => 'publish',
'comment_status' => 'close',
'ping_status' => 'closed'
);
$pid = wp_insert_post($args);
wp_set_object_terms($pid, $tax_name, 'your_tax_slug', false);
For more reference of wp_set_object_terms()
you can check this link.
Hope this helps you.