Skip to content

Instantly share code, notes, and snippets.

@wisecrab
Last active December 24, 2015 17:18
Show Gist options
  • Save wisecrab/eb9d39b35f2c009fe8d3 to your computer and use it in GitHub Desktop.
Save wisecrab/eb9d39b35f2c009fe8d3 to your computer and use it in GitHub Desktop.
Term Meta in WordPress 4.4
<?php
/*
The functions below will work in wordpress 4.4 and greater.
These functions allow you to add, get, and update meta data for terms/taxonomies.
*/
/*
*** Add Meta Data To A Term
*
* @param $term_id int (Required) The id of the term/taxonomy
* @param $meta_key string (Required) The name of the meta info you want to add
* @param $meta_value mixed (Required) The value for the meta information
* @param $unique bool (Optional) Whether to bail if an entry with the same key is found for the term, default is false
*
* @return (int|WP_Error|bool) Meta ID on success. WP_Error when term_id is ambiguous between taxonomies. False on failure.
*
* WP URL: https://developer.wordpress.org/reference/functions/add_term_meta/
*/
add_term_meta( int $term_id, string $meta_key, mixed $meta_value, $unique );
/*
*** Get Meta Data From A Term
*
* @param $term_id int (Required) The id of the term/taxonomy
* @param $key string (Optional) The name of the meta key to retreive
* @param $single bool (Optional) Whether to return a single value. If false, an array of all values matching the $term_id/$key pair will be returned, default is false.
*
* @return (mixed) If $single is false, an array of metadata values. If $single is true, a single metadata value.
*
* WP URL: https://developer.wordpress.org/reference/functions/get_term_meta/
*/
get_term_meta ( int $term_id, string $meta_key = '', bool $single = false )
/*
*** Update Meta Data For A Term
*
* @param $term_id int (Required) The id of the term/taxonomy.
* @param $meta_key string (Required) The name of the meta info you want to update.
* @param $meta_value mixed (Required) The value for the meta information.
* @param $prev_value mixed (Optional) Previous value to check before removing.
*
* @return (int|WP_Error|bool) Meta ID if the key didn't previously exist. True on successful update. WP_Error when term_id is ambiguous
*
* WP URL: https://developer.wordpress.org/reference/functions/update_term_meta/
*/
update_term_meta ( int $term_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment