get_term_meta() – get value from taxonomy meta field

This function is used if you need to get some taxonomy meta data. This function works the same as the get_post_meta() function.

get_term_meta ( $term_id, $key, $single )

$term_id
(number) (required) we specify the taxonomy element ID

$key
(string) (optional) we specify the name of the key of the meta field. If the key is not specified, it will display an array of all meta fields of this taxonomy. Default “.

$single
(logical) (optional) can be:

false – returns all values of arbitrary fields with the specified key as an array, this is what I mean:

true – returns the very first value of an arbitrary field with the specified key as a string;

Returns:

if $single is false, then returns an array of values for the specified meta field.
if $single is true, then returns the value of the specified meta field (the very first).

Example of use:

$termini = get_terms( 'category', 'hide_empty=-1' );
 
foreach( $termini as $termin ){
 
echo $termin_meta = get_term_meta( $termin->term_id, 'my_meta_field', true );
 
}

In this example, we passed through the cycle all taxonomies (posts headings) and derived the result for each rubric (category). To get data about taxonomy elements, we used the get_terms() function.

The most important thing! Do not forget to check that your taxonomy has created meta fields, otherwise you will get an empty array. You can add data using simple functions add_term_meta() or update_term_meta().

You can comment on the article below or ask a question on Uzabila QNA.

Top ↑

Sergei Ermilov

I create web projects, come up with ideas, develop, optimize and promote websites. The first steps in this direction were taken in 2008. If you have any ideas or want to suggest something, then write to me and my team.

Leave a Reply