getTerm($slug, $termFields)

Get all the registered terms for a given taxonomy.

Usage

site()->getTerm($slug, $termFields);

Parameters

ParameterTypeRequiredDescription
$slugstringYesThe slug of a taxonomy
$termFieldsarrayNoA string array of fields, see below for possible options.
Possible Fields
  • term_id – Returns the term’s id
  • id – Shortcut for term_id
  • name – The term’s name
  • title – Same as name
  • slug – The term’s slug
  • url – Returns the result of get_term_link($term)
  • permalink – Same as url
  • term_group – The term’s group
  • taxonomy – The slug of the term’s taxonomy (Will always be the same as the $slug parameter)
  • description – The term’s description, if set
  • parent – The term’s parent
  • count – Cached object count for this term

Any member of WP_Term can be accessed this way.

Returns

TypeDescription
arrayIf fields were provided, values will be returned under those keys. Otherwise an array of WP_Term objects will be returned.

Example

Getting the name and slug for all the terms within the car_type taxonomy.

site()->getTerm('car_type', ['name','slug']);
// Returns
[
[
"name" => "Hatchback",
"slug" => "hatchback"
],
[
"name" => "Hyper Car",
"slug" => "hypercar"
],
[
"name" => "Sports Car",
"slug" => "sportscar"
],
... // And more
]