generate functionTue, 08 Nov 2022

wordpress check if term exists, if not create and attach post

function associate_term_to_post($post_id, $term, $taxonomy, $post_type='post') { $term_id = term_exists( $term, $taxonomy ); $term_id = $term_id['term_id']; if( $term_id == 0 || $term_id == null ) { $term_id = wp_insert_term( $term, $taxonomy ); $term_id = $term_id['term_id']; } wp_set_object_terms( $post_id, $term_id, $taxonomy, true ); }

Questions about programming?Chat with your personal AI assistant