LLMS_Abstract_Generator_Posts::get_term_id( string $term_name, string $tax )

Get a WP Term ID for a term by taxonomy and term name

Description Description

Attempts to find a given term by name first to prevent duplicates during imports.

Top ↑

Parameters Parameters


(string) (Required) Term name.


(string) (Required) Taxonomy slug.

Top ↑

Return Return

(int) The created WP_Term term_id.

Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	protected function get_term_id( $term_name, $tax ) {

		$term = get_term_by( 'name', $term_name, $tax, ARRAY_A );

		// Not found, create it.
		if ( ! $term ) {

			$term = wp_insert_term( $term_name, $tax );

			if ( is_wp_error( $term ) ) {
				throw new Exception( sprintf( __( 'Error creating new term "%s".', 'lifterlms' ), $term_name ), self::ERROR_CREATE_TERM );

			 * Triggered when a new term is generated during an import
			 * @since 4.7.0
			 * @param array  $term Term information array from `wp_insert_term()`.
			 * @param string $tax  Taxonomy name.
			do_action( 'llms_generator_new_term', $term, $tax );


		return $term['term_id'];


Top ↑

Changelog Changelog

Version Description
4.7.0 Moved from LLMS_Generator and updated method access from private to protected. Throws an exception in favor of returning null when an error is encountered.
3.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.