WordPressを読む 60-4 /blog/wp-includes/taxonomy.php 4
2015/02/25
/blog/wp-includes/taxonomy.php 4
関数 get_terms()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | /** * Retrieve the terms in a given taxonomy or list of taxonomies. * * You can fully inject any customizations to the query before it is sent, as * well as control the output with a filter. * * The 'get_terms' filter will be called when the cache has the term and will * pass the found term along with the array of $taxonomies and array of $args. * This filter is also called before the array of terms is passed and will pass * the array of terms, along with the $taxonomies and $args. * * The 'list_terms_exclusions' filter passes the compiled exclusions along with * the $args. * * The 'get_terms_orderby' filter passes the ORDER BY clause for the query * along with the $args array. * * The 'get_terms_fields' filter passes the fields for the SELECT query * along with the $args array. * * @since 2.3.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param string|array $taxonomies Taxonomy name or list of Taxonomy names. * @param array|string $args { * Optional. Array or string of arguments to get terms. * * @type string $orderby Field(s) to order terms by. Accepts term fields, though * empty defaults to 'term_id'. Default 'name'. * @type string $order Whether to order terms in ascending or descending order. * Accepts 'ASC' (ascending) or 'DESC' (descending). * Default 'ASC'. * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts * 1|true or 0|false. Default 1|true. * @type array|string $include Array or comma/space-separated string of term ids to include. * Default empty array. * @type array|string $exclude Array or comma/space-separated string of term ids to exclude. * If $include is non-empty, $exclude is ignored. * Default empty array. * @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude * along with all of their descendant terms. If $include is * non-empty, $exclude_tree is ignored. Default empty array. * @type int $number Maximum number of terms to return. Accepts 1+ or -1 (all). * Default -1. * @type int $offset The number by which to offset the terms query. Default empty. * @type string $fields Term fields to query for. Accepts 'all' (returns an array of * term objects), 'ids' or 'names' (returns an array of integers * or strings, respectively. Default 'all'. * @type string $slug Slug to return term(s) for. Default empty. * @type bool $hierarchical Whether to include terms that have non-empty descendants (even * if $hide_empty is set to true). Default true. * @type string $search Search criteria to match terms. Will be SQL-formatted with * wildcards before and after. Default empty. * @type string $name__like Retrieve terms with criteria by which a term is LIKE $name__like. * Default empty. * @type string $description__like Retrieve terms where the description is LIKE $description__like. * Default empty. * @type bool $pad_counts Whether to pad the quantity of a term's children in the quantity * of each term's "count" object variable. Default false. * @type string $get Whether to return terms regardless of ancestry or whether the terms * are empty. Accepts 'all' or empty (disabled). Default empty. * @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies * are passed, $child_of is ignored. Default 0. * @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty. * @type string $cache_domain Unique cache key to be produced when this query is stored in an * object cache. Default is 'core'. * } * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies * do not exist. */ function get_terms( $taxonomies, $args = '' ) { global $wpdb; $empty_array = array(); $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies ); if ( ! is_array( $taxonomies ) ) { $taxonomies = array( $taxonomies ); } foreach ( $taxonomies as $taxonomy ) { if ( ! taxonomy_exists($taxonomy) ) { $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); return $error; } } $defaults = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $args = wp_parse_args( $args, $defaults ); $args['number'] = absint( $args['number'] ); $args['offset'] = absint( $args['offset'] ); if ( !$single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) || ( '' !== $args['parent'] && 0 !== $args['parent'] ) ) { $args['child_of'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } if ( 'all' == $args['get'] ) { $args['child_of'] = 0; $args['hide_empty'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } /** * Filter the terms query arguments. * * @since 3.1.0 * * @param array $args An array of arguments. * @param string|array $taxonomies A taxonomy or array of taxonomies. */ $args = apply_filters( 'get_terms_args', $args, $taxonomies ); $child_of = $args['child_of']; if ( $child_of ) { $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); if ( ! isset( $hierarchy[ $child_of ] ) ) { return $empty_array; } } $parent = $args['parent']; if ( $parent ) { $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); if ( ! isset( $hierarchy[ $parent ] ) ) { return $empty_array; } } // $args can be whatever, only use the args defined in defaults to compute the key $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key ); $last_changed = wp_cache_get( 'last_changed', 'terms' ); if ( ! $last_changed ) { $last_changed = microtime(); wp_cache_set( 'last_changed', $last_changed, 'terms' ); } $cache_key = "get_terms:$key:$last_changed"; $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { /** * Filter the given taxonomy's terms cache. * * @since 2.3.0 * * @param array $cache Cached array of terms for the given taxonomy. * @param string|array $taxonomies A taxonomy or array of taxonomies. * @param array $args An array of arguments to get terms. */ $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args ); return $cache; } $_orderby = strtolower( $args['orderby'] ); if ( 'count' == $_orderby ) { $orderby = 'tt.count'; } else if ( 'name' == $_orderby ) { $orderby = 't.name'; } else if ( 'slug' == $_orderby ) { $orderby = 't.slug'; } else if ( 'term_group' == $_orderby ) { $orderby = 't.term_group'; } else if ( 'none' == $_orderby ) { $orderby = ''; } elseif ( empty($_orderby) || 'id' == $_orderby ) { $orderby = 't.term_id'; } else { $orderby = 't.name'; } /** * Filter the ORDERBY clause of the terms query. * * @since 2.8.0 * * @param string $orderby ORDERBY clause of the terms query. * @param array $args An array of terms query arguments. * @param string|array $taxonomies A taxonomy or array of taxonomies. */ $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies ); $order = strtoupper( $args['order'] ); if ( ! empty( $orderby ) ) { $orderby = "ORDER BY $orderby"; } else { $order = ''; } if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) { $order = 'ASC'; } $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; $exclude = $args['exclude']; $exclude_tree = $args['exclude_tree']; $include = $args['include']; $inclusions = ''; if ( ! empty( $include ) ) { $exclude = ''; $exclude_tree = ''; $inclusions = implode( ',', wp_parse_id_list( $include ) ); } if ( ! empty( $inclusions ) ) { $inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )'; $where .= $inclusions; } if ( ! empty( $exclude_tree ) ) { $exclude_tree = wp_parse_id_list( $exclude_tree ); $excluded_children = $exclude_tree; foreach ( $exclude_tree as $extrunk ) { $excluded_children = array_merge( $excluded_children, (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) ) ); } $exclusions = implode( ',', array_map( 'intval', $excluded_children ) ); } else { $exclusions = ''; } if ( ! empty( $exclude ) ) { $exterms = wp_parse_id_list( $exclude ); if ( empty( $exclusions ) ) { $exclusions = implode( ',', $exterms ); } else { $exclusions .= ', ' . implode( ',', $exterms ); } } if ( ! empty( $exclusions ) ) { $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')'; } /** * Filter the terms to exclude from the terms query. * * @since 2.3.0 * * @param string $exclusions NOT IN clause of the terms query. * @param array $args An array of terms query arguments. * @param string|array $taxonomies A taxonomy or array of taxonomies. */ $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); if ( ! empty( $exclusions ) ) { $where .= $exclusions; } if ( ! empty( $args['slug'] ) ) { $slug = sanitize_title( $args['slug'] ); $where .= " AND t.slug = '$slug'"; } if ( ! empty( $args['name__like'] ) ) { $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' ); } if ( ! empty( $args['description__like'] ) ) { $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' ); } if ( '' !== $parent ) { $parent = (int) $parent; $where .= " AND tt.parent = '$parent'"; } $hierarchical = $args['hierarchical']; if ( 'count' == $args['fields'] ) { $hierarchical = false; } if ( $args['hide_empty'] && !$hierarchical ) { $where .= ' AND tt.count > 0'; } $number = $args['number']; $offset = $args['offset']; // don't limit the query results when we have to descend the family tree if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { if ( $offset ) { $limits = 'LIMIT ' . $offset . ',' . $number; } else { $limits = 'LIMIT ' . $number; } } else { $limits = ''; } if ( ! empty( $args['search'] ) ) { $like = '%' . $wpdb->esc_like( $args['search'] ) . '%'; $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); } $selects = array(); switch ( $args['fields'] ) { case 'all': $selects = array( 't.*', 'tt.*' ); break; case 'ids': case 'id=>parent': $selects = array( 't.term_id', 'tt.parent', 'tt.count' ); break; case 'names': $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' ); break; case 'count': $orderby = ''; $order = ''; $selects = array( 'COUNT(*)' ); break; case 'id=>name': $selects = array( 't.term_id', 't.name' ); break; case 'id=>slug': $selects = array( 't.term_id', 't.slug' ); break; } $_fields = $args['fields']; /** * Filter the fields to select in the terms query. * * @since 2.8.0 * * @param array $selects An array of fields to select for the terms query. * @param array $args An array of term query arguments. * @param string|array $taxonomies A taxonomy or array of taxonomies. */ $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); /** * Filter the terms query SQL clauses. * * @since 3.1.0 * * @param array $pieces Terms query SQL clauses. * @param string|array $taxonomies A taxonomy or array of taxonomies. * @param array $args An array of terms query arguments. */ $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; if ( 'count' == $_fields ) { $term_count = $wpdb->get_var($query); return $term_count; } $terms = $wpdb->get_results($query); if ( 'all' == $_fields ) { update_term_cache($terms); } if ( empty($terms) ) { wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); /** This filter is documented in wp-includes/taxonomy.php */ $terms = apply_filters( 'get_terms', array(), $taxonomies, $args ); return $terms; } if ( $child_of ) { $children = _get_term_hierarchy( reset( $taxonomies ) ); if ( ! empty( $children ) ) { $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) ); } } // Update term counts to include children. if ( $args['pad_counts'] && 'all' == $_fields ) { _pad_term_counts( $terms, reset( $taxonomies ) ); } // Make sure we show empty categories that have children. if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { foreach ( $terms as $k => $term ) { if ( ! $term->count ) { $children = get_term_children( $term->term_id, reset( $taxonomies ) ); if ( is_array( $children ) ) { foreach ( $children as $child_id ) { $child = get_term( $child_id, reset( $taxonomies ) ); if ( $child->count ) { continue 2; } } } // It really is empty unset($terms[$k]); } } } reset( $terms ); $_terms = array(); if ( 'id=>parent' == $_fields ) { while ( $term = array_shift( $terms ) ) { $_terms[$term->term_id] = $term->parent; } } elseif ( 'ids' == $_fields ) { while ( $term = array_shift( $terms ) ) { $_terms[] = $term->term_id; } } elseif ( 'names' == $_fields ) { while ( $term = array_shift( $terms ) ) { $_terms[] = $term->name; } } elseif ( 'id=>name' == $_fields ) { while ( $term = array_shift( $terms ) ) { $_terms[$term->term_id] = $term->name; } } elseif ( 'id=>slug' == $_fields ) { while ( $term = array_shift( $terms ) ) { $_terms[$term->term_id] = $term->slug; } } if ( ! empty( $_terms ) ) { $terms = $_terms; } if ( $number && is_array( $terms ) && count( $terms ) > $number ) { $terms = array_slice( $terms, $offset, $number ); } wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); /** This filter is documented in wp-includes/taxonomy */ $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); return $terms; } |