get_term_field
Get sanitized Term field.
Description
(string|int|null|WP_Error) get_term_field( (string) $field, (int|WP_Term) $term, (string) $taxonomy = '', (string) $context = 'display' );
The function is for contextual reasons and for simplicity of usage.
Returns (string|int|null|WP_Error)
Will return an empty string if $term is not an object or if $field is not set in $term.
Parameters (4)
- 0. $field (string)
- Term field to fetch.
- 1. $term (int|WP_Term)
- Term ID or object.
- 2. $taxonomy — Optional. (string) =>
''
- Taxonomy Name. Default empty.
- 3. $context — Optional. (string) =>
'display'
- Optional, default is display. Look at
sanitize_term_field(…)
for available options.
Usage
if ( !function_exists( 'get_term_field' ) ) { require_once ABSPATH . WPINC . '/taxonomy.php'; } // Term field to fetch. $field = ''; // Term ID or object. $term = null; // Optional. Taxonomy Name. Default empty. $taxonomy = ''; // Optional, default is display. Look at sanitize_term_field() for available options. $context = 'display'; // NOTICE! Understand what this does before running. $result = get_term_field($field, $term, $taxonomy, $context);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/taxonomy.php
- function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
- $term = get_term( $term, $taxonomy );
- if ( is_wp_error($term) )
- return $term;
- if ( !is_object($term) )
- return '';
- if ( !isset($term->$field) )
- return '';
- return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
- }