In this snippet, we will show you how to use the built-in Tags field to create a WordPress post using a non hierarchical custom taxonomy.
123456789101112131415161718192021add_filter( 'gform_post_data', 'replace_tags_with_custom_taxonomy', 10, 2 );function replace_tags_with_custom_taxonomy( $post_data, $form ) { //only change post type on form id 5 if ( $form['id'] != 5 ) { return $post_data; } //------------------------------------------------------------------------------------ // Replace my_taxonomy with your custom taxonomy name as defined in your register_taxonomy() function call $custom_taxonomy = 'my_taxonomy'; //------------------------------------------------------------------------------------ // Getting tags $tags = implode( ',', $post_data['tags_input'] ); // Array of taxonomy terms keyed by their taxonomy name. $post_data['tax_input'] = array( $custom_taxonomy => $tags ); // Set default tags to empty. $post_data['tags_input'] = null; // Return modified post data with the custom taxonomy terms added. return $post_data;}
IMPORTANT: wp_insert_post() WordPress core function, used to create the post, checks the user』s capabilities before adding custom taxonomies. Therefore the above snippet will work only if the user is logged in and have the capability defined in assign_terms of your custom taxonomy definition.
If you need to set the custom taxonomy for not logged visitors, then you would need to use gform_after_submission hook to get the post_id from the entry object and update the post terms using wp_set_object_terms WP core function.
Create Post with Custom Taxonomy
Create Post with Custom Taxonomy