Custom field to custom taxonomy term?
-
I have this script to save a custom field as a custom taxonomy term. All is working fine, but I would like to extend it a little bit, so that multiple values in the same custom field (seperated by a comma) are saved as different custom taxonomy terms. In example: in a custom field i write ‘value 1, value 2’. I would like those 2 values to be separately saved as a custom taxonomy term. Is there anyone who has a solution for this, based on my code below?
//Custom field to custom taxonomy script $input_terms = array(); $input_terms[] = sanitize_text_field( $_POST['customfieldname1'] ); $input_terms[] = sanitize_text_field( $_POST['customfieldname2'] ); $terms = array(); foreach( $input_terms as $term ) { $existent_term = term_exists( $term, 'mycustomtaxonomy' ); if( $existent_term && $existent_term['term_id'] ) { $term_id = $existent_term['term_id']; } else { $term = wp_insert_term( $term, // the term 'mycustomtaxonomy', // the taxonomy array( 'description'=> 'This is '.$term.'!', 'slug' => $term ) ); if( !is_wp_error($term ) && $term['term_id'] ) { $term_id = $term['term_id']; } } //Fill the array of terms for later use on wp_set_object_terms $terms[] = (int) $term_id; } wp_set_object_terms( $listing_id, $terms, 'mycustomtaxonomy' ); reset();
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Custom field to custom taxonomy term?’ is closed to new replies.