返回介绍

wp_get_object_terms()

发布于 2017-09-11 12:07:04 字数 8766 浏览 1135 评论 0 收藏 0

wp_get_object_terms( int|array $object_ids,  string|array $taxonomies,  array|string $args = array() )

Retrieves the terms associated with the given object(s), in the supplied taxonomies.


description


参数

$object_ids

(int|array) (Required) The ID(s) of the object(s) to retrieve.

$taxonomies

(string|array) (Required) The taxonomies to retrieve terms from.

$args

(array|string) (Optional) See WP_Term_Query::__construct() for supported arguments.

Default value: array()


返回值

(array|WP_Error) The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.


源代码

File: wp-includes/taxonomy.php

function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
	global $wpdb;

	if ( empty( $object_ids ) || empty( $taxonomies ) )
		return array();

	if ( !is_array($taxonomies) )
		$taxonomies = array($taxonomies);

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

	if ( !is_array($object_ids) )
		$object_ids = array($object_ids);
	$object_ids = array_map('intval', $object_ids);

	$args = wp_parse_args( $args );

	/*
	 * When one or more queried taxonomies is registered with an 'args' array,
	 * those params override the `$args` passed to this function.
	 */
	$terms = array();
	if ( count( $taxonomies ) > 1 ) {
		foreach ( $taxonomies as $index => $taxonomy ) {
			$t = get_taxonomy( $taxonomy );
			if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) {
				unset( $taxonomies[ $index ] );
				$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
			}
		}
	} else {
		$t = get_taxonomy( $taxonomies[0] );
		if ( isset( $t->args ) && is_array( $t->args ) ) {
			$args = array_merge( $args, $t->args );
		}
	}

	$args['taxonomy'] = $taxonomies;
	$args['object_ids'] = $object_ids;

	$terms = array_merge( $terms, get_terms( $args ) );

	/**
	 * Filters the terms for a given object or objects.
	 *
	 * @since 4.2.0
	 *
	 * @param array $terms      An array of terms for the given object or objects.
	 * @param array $object_ids Array of object IDs for which `$terms` were retrieved.
	 * @param array $taxonomies Array of taxonomies from which `$terms` were retrieved.
	 * @param array $args       An array of arguments for retrieving terms for the given
	 *                          object(s). See wp_get_object_terms() for details.
	 */
	$terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args );

	$object_ids = implode( ',', $object_ids );
	$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";

	/**
	 * Filters the terms for a given object or objects.
	 *
	 * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
	 * {@see 'get_object_terms'} filter is recommended as an alternative.
	 *
	 * @since 2.8.0
	 *
	 * @param array     $terms      An array of terms for the given object or objects.
	 * @param int|array $object_ids Object ID or array of IDs.
	 * @param string    $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names.
	 * @param array     $args       An array of arguments for retrieving terms for the given object(s).
	 *                              See wp_get_object_terms() for details.
	 */
	return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
}

更新日志

Versiondescription
4.7.0Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments.
4.4.0Introduced $meta_query and $update_term_meta_cache arguments. When $fields is 'all' or 'all_with_object_id', an array of WP_Term objects will be returned.
4.2.0Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of $orderby. Introduced $parent argument.
2.3.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: get_object_terms
  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: wp_get_object_terms()
  • wp-includes/taxonomy.php: wp_get_object_terms
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/taxonomy.php: taxonomy_exists()
  • wp-includes/taxonomy.php: get_taxonomy()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::get_items()
  • wp-admin/includes/export.php: wxr_post_taxonomy()
  • wp-admin/includes/taxonomy.php: get_terms_to_edit()
  • wp-admin/includes/template.php: get_inline_data()
  • wp-admin/includes/template.php: wp_terms_checklist()
  • wp-admin/includes/template.php: wp_popular_terms_checklist()
  • wp-admin/includes/media.php: get_attachment_fields_to_edit()
  • wp-admin/includes/media.php: get_compat_media_markup()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/bookmark.php: wp_get_link_cats()
  • wp-includes/category-template.php: get_the_terms()
  • wp-includes/taxonomy.php: update_object_term_cache()
  • wp-includes/taxonomy.php: get_the_taxonomies()
  • wp-includes/taxonomy.php: is_object_in_term()
  • wp-includes/taxonomy.php: wp_get_object_terms()
  • wp-includes/taxonomy.php: wp_set_object_terms()
  • wp-includes/taxonomy.php: wp_delete_object_term_relationships()
  • wp-includes/taxonomy.php: wp_delete_term()
  • wp-includes/link-template.php: get_adjacent_post()
  • wp-includes/link-template.php: get_boundary_post()
  • wp-includes/nav-menu-template.php: _wp_menu_item_classes_by_context()
  • wp-includes/post.php: _update_term_count_on_transition_post_status()
  • wp-includes/post.php: wp_get_post_categories()
  • wp-includes/post.php: wp_get_post_terms()
  • wp-includes/bookmark.php: get_bookmark()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_post()
  • Show 21 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: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    Returns a list of all ‘product’ taxonomy terms that are applied to $post:

    
    $product_terms = wp_get_object_terms( $post->ID,  'product' );
    
    if ( ! empty( $product_terms ) ) {
    	if ( ! is_wp_error( $product_terms ) ) {
    		echo '<ul>';
    			foreach( $product_terms as $term ) {
    				echo '<li><a href="' . esc_url( get_term_link( $term->slug, 'product' ) ) . '">' . esc_html( $term->name ) . '</a></li>'; 
    			}
    		echo '</ul>';
    	}
    }
    
  2. It’s very important to note that if you wish to get the taxonomy terms for a given post, your first choice should be using get_the_terms() since it uses the WordPress object cache and can potentialy save a lot of extra queries.

    This is specially useful if you’re looping over posts results, because WP_Query by default loads all the terms for the queried posts.

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

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

发布评论

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