^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_objects_in_term()用法示例

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

Retrieve object_ids of valid taxonomy and term.


描述

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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;
}

更新日志

Version 描述
2.3.0 Introduced.

相关函数

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()

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈