Inventory_Presser_Taxonomies::save_taxonomy_term( int $post_id, string $taxonomy_name, string $element_name )

Used by the save_post_{post_type} hook. Create a term relationship between a post and a term. Inserts the term first if it does not exist.


Parameters Parameters

$post_id

(int) (Required)

$taxonomy_name

(string) (Required)

$element_name

(string) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-taxonomies.php

	public static function save_taxonomy_term( $post_id, $taxonomy_name, $element_name ) {
		if ( ! isset( $_POST[ $element_name ] ) ) {
			return;
		}

		$term_slug = sanitize_text_field( $_POST[ $element_name ] );
		if ( '' === $term_slug ) {
			// the user is setting the vehicle type to empty string.
			wp_remove_object_terms( $post_id, self::get_term_slug( $taxonomy_name, $post_id ), $taxonomy_name );
			return;
		}
		$term = get_term_by( 'slug', $term_slug, $taxonomy_name );
		if ( empty( $term ) || is_wp_error( $term ) ) {
			// the term does not exist. create it.
			$term_arr = array(
				'slug'        => sanitize_title( $term_slug ),
				'description' => $term_slug,
				'name'        => $term_slug,
			);
			$id_arr   = wp_insert_term( $term_slug, $taxonomy_name, $term_arr );
			if ( ! is_wp_error( $id_arr ) ) {
				$term->term_id = $id_arr['term_id'];
			}
		}
		$set = wp_set_object_terms( $post_id, $term->term_id, $taxonomy_name, false );
	}