Using WordPress ‘get_term()’ PHP function

The get_term() WordPress PHP function retrieves all term data from the database by term ID.

Usage

To use the get_term() function, pass the term ID and the taxonomy as parameters:

$term = get_term($term_id, $taxonomy);

Parameters

  • $term (int|WP_Term|object) – Required. If integer, term data will be fetched from the database, or from the cache if available. If stdClass object (as in the results of a database query), will apply filters and return a WP_Term object with the $term data. If WP_Term, will return $term.
  • $taxonomy (string) – Optional. Taxonomy name that $term is part of. Default: ”.
  • $output (string) – Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default: OBJECT.
  • $filter (string) – Optional. How to sanitize term fields. Default ‘raw’. Default: ‘raw’.

More information

See WordPress Developer Resources: get_term

Examples

Get term slug

To get the term slug, access the slug property of the term object:

$term = get_term($term_id, $taxonomy);
$slug = $term->slug; // e.g., "term-slug-example"

Get term name

To get the term name, access the name property of the term object:

$term = get_term($term_id, $taxonomy);
$name = $term->name; // e.g., "Term Name Example"

Get term description

To get the term description, access the description property of the term object:

$term = get_term($term_id, $taxonomy);
$description = $term->description; // e.g., "This is a term description."

Get term count

To get the term count, access the count property of the term object:

$term = get_term($term_id, $taxonomy);
$count = $term->count; // e.g., 15

Get term as an associative array

To get the term as an associative array, pass the ARRAY_A parameter for the $output:

$term = get_term($term_id, $taxonomy, ARRAY_A);
// e.g., Array ( [term_id] => 1, [name] => "Term Name Example", [slug] => "term-slug-example" )