
How to Use WP Term Meta
- June 24, 2016
- Leave a comment
The highly anticipated feature WP Term Meta has finally been added to WordPress 4.4. This feature allows users to add meta data to terms (these are individual objects for taxonomies) and then Term meta (secondary data) to the objects within the taxonomies.
Before the Term Meta feature release, only items such as term name, slug and description to the term could be added to meta data. This is often insufficient depending upon the requirements developers have to meet. Unlike posts, comments and users if you want to add additional information to terms, you need to add custom tables in your WordPress database to add meta data. But now, thanks to Term Meta, you don’t have to add any additional tables in your database in order to add additional data to terms.
Term Meta can be used in a variety of situations like associating images or color to categories, or attaching icons to specific categories etc. Like posts, comments and users, Term Meta also has functions that perform create, read, update and delete. These functions are described in this article.
add_term_meta()
This function adds meta data to the term. The usage of this function is as follows:
add_term_meta($term_id, $meta_key, $meta_value, $unique);
- $term_id (Required): ID (integer) of the term to which the meta has to be added.
- $meta_key (Required): The key (string) that term meta data is to be associated.
- $meta_data (Required): The data (mixed) that is associated to a term against the given key.
- $unnique (Optional): The parameter (Boolean) to set the meta key as unique. If it is set to true, the meta key will not be added as it already exists.
update_term_meta()
This function updates the meta data for a certain key. The usage of the function is as follows:
update_term_meta($term_id, $meta_key, $meta_value, $prev_value);
- $term_id (Required): ID (integer) of the term to whose meta has to be updated.
- $meta_key (Required): The key (string) whose term meta data is to be updated.
- $meta_value (Required): The new data (mixed) that has to updated against the meta key for the term.
- $prev_value (Optional): The previous value of data (mixed) that has to be changed against the meta key.
This function can be used instead of add_term_meta() as it checks if the meta key already exists for the term id or not. If not then it will create that meta key itself (using add_term_meta() function).
get_term_meta()
This function is used to retrieve the meta value of a term. The usage of the function is as follows:
get_term_meta($term_id, $meta_key, $single);
- $term_id (Required): ID (integer) of the term whose meta data is to be retrieved.
- $meta_key (Optional): The key (string) whose data is to be returned. If no key is specified, the all the meta data associated with the term will be returned
- $single (Optional): The type of value (Boolean) that has to be returned for meta. If it is sound to be true the meta value will be a single string, if it is false it will return an array.
delete_term_meta()
This function is used to delete Term Meta data associated with certain meta keys. The function usage is as follows:
delete_term_meta($term_id, $meta_key, $meta_value);
- $term_id (Required): ID (integer) of the term to whose meta has to be deleted.
- $meta_key (Required): The key (string) whose term meta data is to be deleted.
- $meta_value (Optional): The meta value (mixed) which has to be deleted. This parameter is only used if a specific meta value has to be deleted.
User Comments