| | 3281 | |
| | 3282 | /** |
| | 3283 | * Retrieve term meta field for a term. |
| | 3284 | * |
| | 3285 | * @since 3.5.0 |
| | 3286 | * @link http://codex.wordpress.org/Function_Reference/get_term_meta |
| | 3287 | * |
| | 3288 | * @param int $term_id Term ID. |
| | 3289 | * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. |
| | 3290 | * @param bool $single Whether to return a single value. |
| | 3291 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
| | 3292 | * is true. |
| | 3293 | */ |
| | 3294 | function get_term_meta( $term_id, $key = '', $single = FALSE ) { |
| | 3295 | return get_metadata( 'term', $term_id, $key, $single ); |
| | 3296 | } |
| | 3297 | |
| | 3298 | /** |
| | 3299 | * Update term meta field based on term ID. |
| | 3300 | * |
| | 3301 | * Use the $prev_value parameter to differentiate between meta fields with the |
| | 3302 | * same key and term ID. |
| | 3303 | * |
| | 3304 | * If the meta field for the term does not exist, it will be added. |
| | 3305 | * |
| | 3306 | * @since 3.5.0 |
| | 3307 | * @uses $wpdb |
| | 3308 | * @link http://codex.wordpress.org/Function_Reference/update_term_meta |
| | 3309 | * |
| | 3310 | * @param int $term_id Term ID. |
| | 3311 | * @param string $meta_key Metadata key. |
| | 3312 | * @param mixed $meta_value Metadata value. |
| | 3313 | * @param mixed $prev_value Optional. Previous value to check before removing. |
| | 3314 | * @return bool False on failure, true if success. |
| | 3315 | */ |
| | 3316 | function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) { |
| | 3317 | |
| | 3318 | return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value ); |
| | 3319 | } |
| | 3320 | |
| | 3321 | /** |
| | 3322 | * Delete everything from term meta matching meta key. |
| | 3323 | * |
| | 3324 | * @since 3.5.0 |
| | 3325 | * @uses $wpdb |
| | 3326 | * |
| | 3327 | * @param string $term_meta_key Key to search for when deleting. |
| | 3328 | * @return bool Whether the term meta key was deleted from the database |
| | 3329 | */ |
| | 3330 | function delete_term_meta_by_key( $term_meta_key ) { |
| | 3331 | return delete_metadata( 'term', null, $term_meta_key, '', TRUE ); |
| | 3332 | } |
| | 3333 | |
| | 3334 | /** |
| | 3335 | * Retrieve term meta fields, based on term ID. |
| | 3336 | * |
| | 3337 | * The term meta fields are retrieved from the cache where possible, |
| | 3338 | * so the function is optimized to be called more than once. |
| | 3339 | * |
| | 3340 | * @since 3.5.0 |
| | 3341 | * @link http://codex.wordpress.org/Function_Reference/get_term_custom |
| | 3342 | * |
| | 3343 | * @param int $term_id term ID. |
| | 3344 | * @return array |
| | 3345 | */ |
| | 3346 | function get_term_custom( $term_id = 0 ) { |
| | 3347 | $term_id = absint( $term_id ); |
| | 3348 | if ( ! $term_id ) |
| | 3349 | $term_id = get_the_ID(); |
| | 3350 | |
| | 3351 | return get_term_meta( $term_id ); |
| | 3352 | } |
| | 3353 | |
| | 3354 | /** |
| | 3355 | * Retrieve meta field names for a term. |
| | 3356 | * |
| | 3357 | * If there are no meta fields, then nothing (null) will be returned. |
| | 3358 | * |
| | 3359 | * @since 3.5.0 |
| | 3360 | * @link http://codex.wordpress.org/Function_Reference/get_term_custom_keys |
| | 3361 | * |
| | 3362 | * @param int $term_id term ID |
| | 3363 | * @return array|null Either array of the keys, or null if keys could not be retrieved. |
| | 3364 | */ |
| | 3365 | function get_term_custom_keys( $term_id = 0 ) { |
| | 3366 | $custom = get_term_custom( $term_id ); |
| | 3367 | |
| | 3368 | if ( ! is_array( $custom ) ) |
| | 3369 | return; |
| | 3370 | |
| | 3371 | if ( $keys = array_keys( $custom ) ) |
| | 3372 | return $keys; |
| | 3373 | } |
| | 3374 | |
| | 3375 | /** |
| | 3376 | * Retrieve values for a custom term field. |
| | 3377 | * |
| | 3378 | * The parameters must not be considered optional. All of the term meta fields |
| | 3379 | * will be retrieved and only the meta field key values returned. |
| | 3380 | * |
| | 3381 | * @since 3.5.0 |
| | 3382 | * @link http://codex.wordpress.org/Function_Reference/get_term_custom_values |
| | 3383 | * |
| | 3384 | * @param string $key Meta field key. |
| | 3385 | * @param int $term_id Term ID |
| | 3386 | * @return array Meta field values. |
| | 3387 | */ |
| | 3388 | function get_term_custom_values( $key = '', $term_id = 0 ) { |
| | 3389 | if ( ! $key ) |
| | 3390 | return null; |
| | 3391 | |
| | 3392 | $custom = get_term_custom( $term_id ); |
| | 3393 | |
| | 3394 | return isset( $custom[ $key ] ) ? $custom[ $key ] : null; |
| | 3395 | } |
| | 3396 | No newline at end of file |