返回介绍

get_term()

发布于 2017-09-11 00:20:39 字数 11457 浏览 1100 评论 0 收藏 0

get_term( int|WP_Term|object $term,  string $taxonomy = '',  string $output = OBJECT,  string $filter = 'raw' )

Get all Term data from database by Term ID.


description

The usage of the get_term function is to apply filters to a term object. It is possible to get a term object from the database before applying the filters.

$term ID must be part of $taxonomy, to get from the database. Failure, might be able to be captured by the hooks. Failure would be the same value as $wpdb returns for the get_row method.

There are two hooks, one is specifically for each term, named ‘get_term’, and the second is for the taxonomy name, ‘term_$taxonomy’. Both hooks gets the term object, and the taxonomy name as parameters. Both hooks are expected to return a Term object.

‘get_term’ hook – Takes two parameters the term Object and the taxonomy name. Must return term object. Used in get_term() as a catch-all filter for every $term.

‘get_$taxonomy’ hook – Takes two parameters the term Object and the taxonomy name. Must return term object. $taxonomy will be the taxonomy name, so for example, if ‘category’, it would be ‘get_category’ as the filter name. Useful for custom taxonomies or plugging into default taxonomies.


参数

$term

(int|WP_Term|object) (Required) If integer, term data will be fetched from the database, or from the cache if available. If stdClass object (as in the results of a database query), will apply filters and return a WP_Term object corresponding to the $term data. If WP_Term, will return $term.

$taxonomy

(string) (Optional) Taxonomy name that $term is part of.

Default value: ''

$output

(string) (Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$filter

(string) (Optional) default is raw or no WordPress defined filter will applied.

Default value: 'raw'


返回值

(array|WP_Term|WP_Error|null) Object of the type specified by $output on success. When $output is 'OBJECT', a WP_Term instance is returned. If taxonomy does not exist, a WP_Error is returned. Returns null for miscellaneous failure.


源代码

File: wp-includes/taxonomy.php

function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
	if ( empty( $term ) ) {
		return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
	}

	if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	if ( $term instanceof WP_Term ) {
		$_term = $term;
	} elseif ( is_object( $term ) ) {
		if ( empty( $term->filter ) || 'raw' === $term->filter ) {
			$_term = sanitize_term( $term, $taxonomy, 'raw' );
			$_term = new WP_Term( $_term );
		} else {
			$_term = WP_Term::get_instance( $term->term_id );
		}
	} else {
		$_term = WP_Term::get_instance( $term, $taxonomy );
	}

	if ( is_wp_error( $_term ) ) {
		return $_term;
	} elseif ( ! $_term ) {
		return null;
	}

	/**
	 * Filters a term.
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` can now also be a WP_Term object.
	 *
	 * @param int|WP_Term $_term    Term object or ID.
	 * @param string      $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( 'get_term', $_term, $taxonomy );

	/**
	 * Filters a taxonomy.
	 *
	 * The dynamic portion of the filter name, `$taxonomy`, refers
	 * to the taxonomy slug.
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` can now also be a WP_Term object.
	 *
	 * @param int|WP_Term $_term    Term object or ID.
	 * @param string      $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );

	// Bail if a filter callback has changed the type of the `$_term` object.
	if ( ! ( $_term instanceof WP_Term ) ) {
		return $_term;
	}

	// Sanitize term, according to the specified filter.
	$_term->filter( $filter );

	if ( $output == ARRAY_A ) {
		return $_term->to_array();
	} elseif ( $output == ARRAY_N ) {
		return array_values( $_term->to_array() );
	}

	return $_term;
}

更新日志

Versiondescription
4.4.0Converted to return a WP_Term object if $output is OBJECT. The $taxonomy parameter was made optional.
2.3.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-term.php: WP_Term::__construct()
  • wp-includes/class-wp-term.php: WP_Term::get_instance()
  • wp-includes/l10n.php: __()
  • wp-includes/taxonomy.php: sanitize_term()
  • wp-includes/taxonomy.php: get_term
  • wp-includes/taxonomy.php: get_{$taxonomy}
  • wp-includes/taxonomy.php: taxonomy_exists()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/category-template.php: get_term_parents_list()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::get_term()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::prepare_links()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::prepare_item_for_database()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_assign_terms_permission()
  • wp-includes/class-wp-term-query.php: WP_Term_Query::get_terms()
  • wp-admin/includes/class-wp-links-list-table.php: WP_Links_List_Table::column_categories()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/taxonomy.php: get_category_to_edit()
  • wp-admin/includes/taxonomy.php: wp_update_category()
  • wp-admin/includes/template.php: wp_terms_checklist()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_menu_item()
  • wp-admin/includes/ajax-actions.php: wp_ajax_inline_save_tax()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_add_hierarchical_term()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_tag()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_tag()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::column_name()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::_rows()
  • wp-admin/includes/nav-menu.php: _wp_ajax_menu_quick_search()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/class-walker-category.php: Walker_Category::start_el()
  • wp-includes/category-template.php: get_the_category_by_ID()
  • wp-includes/class-wp-query.php: WP_Query::get_queried_object()
  • wp-includes/category.php: get_cat_name()
  • wp-includes/category.php: get_tag()
  • wp-includes/category.php: get_category()
  • wp-includes/category.php: get_category_by_path()
  • wp-includes/taxonomy.php: wp_get_term_taxonomy_parent_id()
  • wp-includes/taxonomy.php: get_object_term_cache()
  • wp-includes/taxonomy.php: _get_term_children()
  • wp-includes/taxonomy.php: get_term_link()
  • wp-includes/taxonomy.php: get_ancestors()
  • wp-includes/taxonomy.php: wp_unique_term_slug()
  • wp-includes/taxonomy.php: wp_update_term()
  • wp-includes/taxonomy.php: wp_delete_term()
  • wp-includes/taxonomy.php: get_term_by()
  • wp-includes/taxonomy.php: get_term_field()
  • wp-includes/taxonomy.php: get_term_to_edit()
  • wp-includes/taxonomy.php: term_is_ancestor_of()
  • wp-includes/link-template.php: get_term_feed_link()
  • wp-includes/link-template.php: get_edit_term_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/nav-menu.php: wp_get_nav_menu_object()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_newTerm()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_editTerm()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deleteTerm()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getTerm()
  • Show 44 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Examples
    Get Term offers some handy information, but unfortunately lacks a link value.

    
    $term = get_term( $term_id, $taxonomy );
    

    Gives you term slug: e.g.: term-slug-example

    
    $slug = $term->slug;
    

    Gives you term name: e.g. Term Name Example

    
    $name = $term->name;
    

    Gives you term description: e.g. This is my new cool custom term.

    
    $desc = $term->description;
    
  2. get_term() utilizes the WP Object Cache to store previously-fetched term data. This helps avoid subsequent data I/O calls from the database to read term data. For example:

    
    $term = get_term( 1 , 'store' );
    echo $term->name;
    $term = get_term( 1 , ' store' );
    echo $term->slug;
    

    This overly-simple example will only perform a single select query on the database. The second get_term will use the WP Object Cache to fetch the previous term object from memory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文