get_objects_in_term
函数
get_objects_in_term ( $term_ids, $taxonomies, $args = array() )
- 参数
-
-
(int|int[])
$term_ids
Term ID or array of term IDs of terms that will be used.- Required: 是
-
(string|string[])
$taxonomies
String of taxonomy name or Array of string values of taxonomy names.- Required: 是
-
(array|string)
$args
Change the order of the object IDs, either ASC or DESC.- Required: 否
- Default: array()
-
(int|int[])
- 返回值
-
- (string[]|WP_Error) An array of object IDs as numeric strings on success, WP_Error if the taxonomy does not exist.
- 定义位置
-
-
wp-includes/taxonomy.php
, line 822
-
wp-includes/taxonomy.php
- 引入
- 2.3.0
- 弃用
- –
检索有效分类法和术语的对象ID。
`$taxonomies`的字符串必须存在,这个函数才会继续。如果找不到有效的分类法,它将返回一个WP_Error。
`$terms`没有像`$taxonomies`那样被检查,但是仍然需要存在对象ID才能被返回。
通过使用`$args`的ASC或DESC数组,可以改变对象ID的返回顺序。该值应该在名为’order’的键中。
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 ) . "'";
$sql = "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";
$last_changed = wp_cache_get_last_changed( 'terms' );
$cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );
if ( false === $cache ) {
$object_ids = $wpdb->get_col( $sql );
wp_cache_set( $cache_key, $object_ids, 'terms' );
} else {
$object_ids = (array) $cache;
}
if ( ! $object_ids ) {
return array();
}
return $object_ids;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。


