get_term_by()
get_term_by( string $field, string|int $value, string $…
get_term_by( string $field, string|int $value, string $taxonomy = ”, string $output = OBJECT, string $filter = ‘raw’ )
按术语字段和数据从数据库中获取所有术语数据。
Get all Term data from database by Term field and data.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
另见函数 sanitize_term_field()
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$field | (string) | 必需 | “slug”、“name”、“id”(term-id)或“term-taxonomy-id” |
$value | (string | int) | 必需 | 搜索此术语值 |
$taxonomy | (string) | 可选 | 分类法名称。可选,如果$field是’term_taxonomy_id’。 |
$output | (string) | 可选 | 所需的返回类型。数组或数组中的一个,分别对应于WP术语对象、关联数组或数值数组。 |
$filter | (string) | 可选 | 默认为raw或不应用WordPress定义的过滤器。 |
返回(Return)
(WP|Term|array|false)WP_Term实例(WP|Term|array|false)成功时。如果$taxonomy不存在或找不到$term,则返回false。
源码(Source)
/** * Get all Term data from database by Term field and data. * * Warning: $value is not escaped for 'name' $field. You must do it yourself, if * required. * * The default $field is 'id', therefore it is possible to also use null for * field, but not recommended that you do so. * * If $value does not exist, the return value will be false. If $taxonomy exists * and $field and $value combinations exist, the Term will be returned. * * @todo Better formatting for DocBlock. * * @since 2.3.0 * * @global wpdb $wpdb WordPress database abstraction object. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' * @param string|int $value Search for this term value * @param string $taxonomy Taxonomy Name * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N * @param string $filter Optional, default is raw or no WordPress defined filter will applied. * @return object|array|null|WP_Error|false Term Row from database. * Will return false if $taxonomy does not exist or $term was not found. */ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { global $wpdb; if ( ! taxonomy_exists($taxonomy) ) return false; if ( 'slug' == $field ) { $field = 't.slug'; $value = sanitize_title($value); if ( empty($value) ) return false; } elseif ( 'name' == $field ) { // Assume already escaped $value = wp_unslash($value); $field = 't.name'; } elseif ( 'term_taxonomy_id' == $field ) { $value = (int) $value; $field = 'tt.term_taxonomy_id'; } else { $term = get_term( (int) $value, $taxonomy, $output, $filter ); if ( is_wp_error( $term ) ) $term = false; return $term; } $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) ); if ( ! $term ) return false; wp_cache_add( $term->term_id, $term, $taxonomy ); /** This filter is documented in wp-includes/taxonomy.php */ $term = apply_filters( 'get_term', $term, $taxonomy ); /** This filter is documented in wp-includes/taxonomy.php */ $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); $term = sanitize_term($term, $taxonomy, $filter); if ( $output == OBJECT ) { return $term; } elseif ( $output == ARRAY_A ) { return get_object_vars($term); } elseif ( $output == ARRAY_N ) { return array_values(get_object_vars($term)); } else { return $term; } }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
4.4.0 | wp-includes/taxonomy.php:928 | 16 | 4 |
笔记(Notes)
get_term_by()返回单个WP_term对象。由于核心版本从v4.1到4.3的变化,现在可以使用多个术语来匹配提供的名称或slug参数。返回的WP_Term对象将是mySQL找到的第一个匹配项,没有迹象表明可能存在其他匹配项。如果应用程序中可能有多个具有相同名称或slug的术语,则应使用get_terms()而不是get_term_by()。
按类别名称检索类别的示例:
实例
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!