返回介绍

get_objects_in_term()

发布于 2017-09-10 23:38:37 字数 3704 浏览 951 评论 0 收藏 0

get_objects_in_term( int|array $term_ids,  string|array $taxonomies,  array|string $args = array() )

Retrieve object_ids of valid taxonomy and term.


description

The strings of $taxonomies must exist before this function will continue. On failure of finding a valid taxonomy, it will return an WP_Error class, kind of like Exceptions in PHP 5, except you can’t catch them. Even so, you can still test for the WP_Error class and get the error message.

The $terms aren’t checked the same as $taxonomies, but still need to exist for $object_ids to be returned.

It is possible to change the order that object_ids is returned by either using PHP sort family functions or using the database by using $args with either ASC or DESC array. The value should be in the key named ‘order’.


参数

$term_ids

(int|array) (Required) Term id or array of term ids of terms that will be used.

$taxonomies

(string|array) (Required) String of taxonomy name or Array of string values of taxonomy names.

$args

(array|string) (Optional) Change the order of the object_ids, either ASC or DESC.

Default value: array()


返回值

(WP_Error|array) If the taxonomy does not exist, then WP_Error will be returned. On success. the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.


源代码

File: wp-includes/taxonomy.php

function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
	global $wpdb;

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

	$defaults = array( 'order' => 'ASC' );
	$args = wp_parse_args( $args, $defaults );

	$order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';

	$term_ids = array_map('intval', $term_ids );

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

	$object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");

	if ( ! $object_ids ){
		return array();
	}
	return $object_ids;
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: taxonomy_exists()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/class-wp-error.php: WP_Error::__construct()

Used By

  • wp-includes/nav-menu.php: wp_delete_nav_menu()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items()

User Contributed Notes

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

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

发布评论

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