WP_Term_Query

Class used for querying terms.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-term-query.php  
  1. class WP_Term_Query { 
  2.  
  3. /** 
  4. * SQL string used to perform database query. 
  5. * @since 4.6.0 
  6. * @access public 
  7. * @var string 
  8. */ 
  9. public $request; 
  10.  
  11. /** 
  12. * Metadata query container. 
  13. * @since 4.6.0 
  14. * @access public 
  15. * @var object WP_Meta_Query 
  16. */ 
  17. public $meta_query = false; 
  18.  
  19. /** 
  20. * Metadata query clauses. 
  21. * @since 4.6.0 
  22. * @access protected 
  23. * @var array 
  24. */ 
  25. protected $meta_query_clauses; 
  26.  
  27. /** 
  28. * SQL query clauses. 
  29. * @since 4.6.0 
  30. * @access protected 
  31. * @var array 
  32. */ 
  33. protected $sql_clauses = array( 
  34. 'select' => '',  
  35. 'from' => '',  
  36. 'where' => array(),  
  37. 'orderby' => '',  
  38. 'limits' => '',  
  39. ); 
  40.  
  41. /** 
  42. * Query vars set by the user. 
  43. * @since 4.6.0 
  44. * @access public 
  45. * @var array 
  46. */ 
  47. public $query_vars; 
  48.  
  49. /** 
  50. * Default values for query vars. 
  51. * @since 4.6.0 
  52. * @access public 
  53. * @var array 
  54. */ 
  55. public $query_var_defaults; 
  56.  
  57. /** 
  58. * List of terms located by the query. 
  59. * @since 4.6.0 
  60. * @access public 
  61. * @var array 
  62. */ 
  63. public $terms; 
  64.  
  65. /** 
  66. * Constructor. 
  67. * Sets up the term query, based on the query vars passed. 
  68. * @since 4.6.0 
  69. * @since 4.6.0 Introduced 'term_taxonomy_id' parameter. 
  70. * @access public 
  71. * @param string|array $query { 
  72. * Optional. Array or query string of term query parameters. Default empty. 
  73. * @type string|array $taxonomy Taxonomy name, or array of taxonomies, to which results should 
  74. * be limited. 
  75. * @type string $orderby Field(s) to order terms by. Accepts term fields ('name',  
  76. * 'slug', 'term_group', 'term_id', 'id', 'description'),  
  77. * 'count' for term taxonomy count, 'include' to match the 
  78. * 'order' of the $include param, 'meta_value', 'meta_value_num',  
  79. * the value of `$meta_key`, the array keys of `$meta_query`, or 
  80. * 'none' to omit the ORDER BY clause. Defaults to 'name'. 
  81. * @type string $order Whether to order terms in ascending or descending order. 
  82. * Accepts 'ASC' (ascending) or 'DESC' (descending). 
  83. * Default 'ASC'. 
  84. * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts 
  85. * 1|true or 0|false. Default 1|true. 
  86. * @type array|string $include Array or comma/space-separated string of term ids to include. 
  87. * Default empty array. 
  88. * @type array|string $exclude Array or comma/space-separated string of term ids to exclude. 
  89. * If $include is non-empty, $exclude is ignored. 
  90. * Default empty array. 
  91. * @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude 
  92. * along with all of their descendant terms. If $include is 
  93. * non-empty, $exclude_tree is ignored. Default empty array. 
  94. * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any 
  95. * positive number. Default ''|0 (all). 
  96. * @type int $offset The number by which to offset the terms query. Default empty. 
  97. * @type string $fields Term fields to query for. Accepts 'all' (returns an array of 
  98. * complete term objects), 'ids' (returns an array of ids),  
  99. * 'id=>parent' (returns an associative array with ids as keys,  
  100. * parent term IDs as values), 'names' (returns an array of term 
  101. * names), 'count' (returns the number of matching terms),  
  102. * 'id=>name' (returns an associative array with ids as keys,  
  103. * term names as values), or 'id=>slug' (returns an associative 
  104. * array with ids as keys, term slugs as values). Default 'all'. 
  105. * @type bool $count Whether to return a term count (true) or array of term objects 
  106. * (false). Will take precedence over `$fields` if true. 
  107. * Default false. 
  108. * @type string|array $name Optional. Name or array of names to return term(s) for. 
  109. * Default empty. 
  110. * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. 
  111. * Default empty. 
  112. * @type int|array $term_taxonomy_id Optional. Term taxonomy ID, or array of term taxonomy IDs,  
  113. * to match when querying terms. 
  114. * @type bool $hierarchical Whether to include terms that have non-empty descendants (even 
  115. * if $hide_empty is set to true). Default true. 
  116. * @type string $search Search criteria to match terms. Will be SQL-formatted with 
  117. * wildcards before and after. Default empty. 
  118. * @type string $name__like Retrieve terms with criteria by which a term is LIKE 
  119. * `$name__like`. Default empty. 
  120. * @type string $description__like Retrieve terms where the description is LIKE 
  121. * `$description__like`. Default empty. 
  122. * @type bool $pad_counts Whether to pad the quantity of a term's children in the 
  123. * quantity of each term's "count" object variable. 
  124. * Default false. 
  125. * @type string $get Whether to return terms regardless of ancestry or whether the 
  126. * terms are empty. Accepts 'all' or empty (disabled). 
  127. * Default empty. 
  128. * @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies 
  129. * are passed, $child_of is ignored. Default 0. 
  130. * @type int|string $parent Parent term ID to retrieve direct-child terms of. 
  131. * Default empty. 
  132. * @type bool $childless True to limit results to terms that have no children. 
  133. * This parameter has no effect on non-hierarchical taxonomies. 
  134. * Default false. 
  135. * @type string $cache_domain Unique cache key to be produced when this query is stored in 
  136. * an object cache. Default is 'core'. 
  137. * @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true. 
  138. * @type array $meta_query Optional. Meta query clauses to limit retrieved terms by. 
  139. * See `WP_Meta_Query`. Default empty. 
  140. * @type string $meta_key Limit terms to those matching a specific metadata key. 
  141. * Can be used in conjunction with `$meta_value`. 
  142. * @type string $meta_value Limit terms to those matching a specific metadata value. 
  143. * Usually used in conjunction with `$meta_key`. 
  144. * } 
  145. */ 
  146. public function __construct( $query = '' ) { 
  147. $this->query_var_defaults = array( 
  148. 'taxonomy' => null,  
  149. 'orderby' => 'name',  
  150. 'order' => 'ASC',  
  151. 'hide_empty' => true,  
  152. 'include' => array(),  
  153. 'exclude' => array(),  
  154. 'exclude_tree' => array(),  
  155. 'number' => '',  
  156. 'offset' => '',  
  157. 'fields' => 'all',  
  158. 'count' => false,  
  159. 'name' => '',  
  160. 'slug' => '',  
  161. 'term_taxonomy_id' => '',  
  162. 'hierarchical' => true,  
  163. 'search' => '',  
  164. 'name__like' => '',  
  165. 'description__like' => '',  
  166. 'pad_counts' => false,  
  167. 'get' => '',  
  168. 'child_of' => 0,  
  169. 'parent' => '',  
  170. 'childless' => false,  
  171. 'cache_domain' => 'core',  
  172. 'update_term_meta_cache' => true,  
  173. 'meta_query' => '',  
  174. ); 
  175.  
  176. if ( ! empty( $query ) ) { 
  177. $this->query( $query ); 
  178.  
  179. /** 
  180. * Parse arguments passed to the term query with default query parameters. 
  181. * @since 4.6.0 
  182. * @access public 
  183. * @param string|array $query WP_Term_Query arguments. See WP_Term_Query::__construct() 
  184. */ 
  185. public function parse_query( $query = '' ) { 
  186. if ( empty( $query ) ) { 
  187. $query = $this->query_vars; 
  188.  
  189. $taxonomies = isset( $query['taxonomy'] ) ? (array) $query['taxonomy'] : null; 
  190.  
  191. /** 
  192. * Filters the terms query default arguments. 
  193. * Use {@see 'get_terms_args'} to filter the passed arguments. 
  194. * @since 4.4.0 
  195. * @param array $defaults An array of default get_terms() arguments. 
  196. * @param array $taxonomies An array of taxonomies. 
  197. */ 
  198. $this->query_var_defaults = apply_filters( 'get_terms_defaults', $this->query_var_defaults, $taxonomies ); 
  199.  
  200. $query = wp_parse_args( $query, $this->query_var_defaults ); 
  201.  
  202. $query['number'] = absint( $query['number'] ); 
  203. $query['offset'] = absint( $query['offset'] ); 
  204.  
  205. // 'parent' overrides 'child_of'. 
  206. if ( 0 < intval( $query['parent'] ) ) { 
  207. $query['child_of'] = false; 
  208.  
  209. if ( 'all' == $query['get'] ) { 
  210. $query['childless'] = false; 
  211. $query['child_of'] = 0; 
  212. $query['hide_empty'] = 0; 
  213. $query['hierarchical'] = false; 
  214. $query['pad_counts'] = false; 
  215.  
  216. $query['taxonomy'] = $taxonomies; 
  217.  
  218. /** 
  219. * Filters the terms query arguments. 
  220. * @since 3.1.0 
  221. * @param array $args An array of get_terms() arguments. 
  222. * @param array $taxonomies An array of taxonomies. 
  223. */ 
  224. $this->query_vars = apply_filters( 'get_terms_args', $query, $taxonomies ); 
  225.  
  226. /** 
  227. * Fires after term query vars have been parsed. 
  228. * @since 4.6.0 
  229. * @param WP_Term_Query $this Current instance of WP_Term_Query. 
  230. */ 
  231. do_action( 'parse_term_query', $this ); 
  232.  
  233. /** 
  234. * Sets up the query for retrieving terms. 
  235. * @since 4.6.0 
  236. * @access public 
  237. * @param string|array $query Array or URL query string of parameters. 
  238. * @return array|int List of terms, or number of terms when 'count' is passed as a query var. 
  239. */ 
  240. public function query( $query ) { 
  241. $this->query_vars = wp_parse_args( $query ); 
  242. return $this->get_terms(); 
  243.  
  244. /** 
  245. * Get terms, based on query_vars. 
  246. * @param 4.6.0 
  247. * @access public 
  248. * @global wpdb $wpdb WordPress database abstraction object. 
  249. * @return array 
  250. */ 
  251. public function get_terms() { 
  252. global $wpdb; 
  253.  
  254. $this->parse_query( $this->query_vars ); 
  255. $args = $this->query_vars; 
  256.  
  257. // Set up meta_query so it's available to 'pre_get_terms'. 
  258. $this->meta_query = new WP_Meta_Query(); 
  259. $this->meta_query->parse_query_vars( $args ); 
  260.  
  261. /** 
  262. * Fires before terms are retrieved. 
  263. * @since 4.6.0 
  264. * @param WP_Term_Query $this Current instance of WP_Term_Query. 
  265. */ 
  266. do_action( 'pre_get_terms', $this ); 
  267.  
  268. $taxonomies = $args['taxonomy']; 
  269.  
  270. // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. 
  271. $has_hierarchical_tax = false; 
  272. if ( $taxonomies ) { 
  273. foreach ( $taxonomies as $_tax ) { 
  274. if ( is_taxonomy_hierarchical( $_tax ) ) { 
  275. $has_hierarchical_tax = true; 
  276.  
  277. if ( ! $has_hierarchical_tax ) { 
  278. $args['hierarchical'] = false; 
  279. $args['pad_counts'] = false; 
  280.  
  281. // 'parent' overrides 'child_of'. 
  282. if ( 0 < intval( $args['parent'] ) ) { 
  283. $args['child_of'] = false; 
  284.  
  285. if ( 'all' == $args['get'] ) { 
  286. $args['childless'] = false; 
  287. $args['child_of'] = 0; 
  288. $args['hide_empty'] = 0; 
  289. $args['hierarchical'] = false; 
  290. $args['pad_counts'] = false; 
  291.  
  292. /** 
  293. * Filters the terms query arguments. 
  294. * @since 3.1.0 
  295. * @param array $args An array of get_terms() arguments. 
  296. * @param array $taxonomies An array of taxonomies. 
  297. */ 
  298. $args = apply_filters( 'get_terms_args', $args, $taxonomies ); 
  299.  
  300. // Avoid the query if the queried parent/child_of term has no descendants. 
  301. $child_of = $args['child_of']; 
  302. $parent = $args['parent']; 
  303.  
  304. if ( $child_of ) { 
  305. $_parent = $child_of; 
  306. } elseif ( $parent ) { 
  307. $_parent = $parent; 
  308. } else { 
  309. $_parent = false; 
  310.  
  311. if ( $_parent ) { 
  312. $in_hierarchy = false; 
  313. foreach ( $taxonomies as $_tax ) { 
  314. $hierarchy = _get_term_hierarchy( $_tax ); 
  315.  
  316. if ( isset( $hierarchy[ $_parent ] ) ) { 
  317. $in_hierarchy = true; 
  318.  
  319. if ( ! $in_hierarchy ) { 
  320. return array(); 
  321.  
  322. $orderby = $this->parse_orderby( $this->query_vars['orderby'] ); 
  323. if ( $orderby ) { 
  324. $orderby = "ORDER BY $orderby"; 
  325.  
  326. $order = $this->parse_order( $this->query_vars['order'] ); 
  327.  
  328. if ( $taxonomies ) { 
  329. $this->sql_clauses['where']['taxonomy'] = "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')"; 
  330.  
  331. $exclude = $args['exclude']; 
  332. $exclude_tree = $args['exclude_tree']; 
  333. $include = $args['include']; 
  334.  
  335. $inclusions = ''; 
  336. if ( ! empty( $include ) ) { 
  337. $exclude = ''; 
  338. $exclude_tree = ''; 
  339. $inclusions = implode( ', ', wp_parse_id_list( $include ) ); 
  340.  
  341. if ( ! empty( $inclusions ) ) { 
  342. $this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )'; 
  343.  
  344. $exclusions = array(); 
  345. if ( ! empty( $exclude_tree ) ) { 
  346. $exclude_tree = wp_parse_id_list( $exclude_tree ); 
  347. $excluded_children = $exclude_tree; 
  348. foreach ( $exclude_tree as $extrunk ) { 
  349. $excluded_children = array_merge( 
  350. $excluded_children,  
  351. (array) get_terms( $taxonomies[0], array( 
  352. 'child_of' => intval( $extrunk ),  
  353. 'fields' => 'ids',  
  354. 'hide_empty' => 0 
  355. ) ) 
  356. ); 
  357. $exclusions = array_merge( $excluded_children, $exclusions ); 
  358.  
  359. if ( ! empty( $exclude ) ) { 
  360. $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions ); 
  361.  
  362. // 'childless' terms are those without an entry in the flattened term hierarchy. 
  363. $childless = (bool) $args['childless']; 
  364. if ( $childless ) { 
  365. foreach ( $taxonomies as $_tax ) { 
  366. $term_hierarchy = _get_term_hierarchy( $_tax ); 
  367. $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions ); 
  368.  
  369. if ( ! empty( $exclusions ) ) { 
  370. $exclusions = 't.term_id NOT IN (' . implode( ', ', array_map( 'intval', $exclusions ) ) . ')'; 
  371. } else { 
  372. $exclusions = ''; 
  373.  
  374. /** 
  375. * Filters the terms to exclude from the terms query. 
  376. * @since 2.3.0 
  377. * @param string $exclusions `NOT IN` clause of the terms query. 
  378. * @param array $args An array of terms query arguments. 
  379. * @param array $taxonomies An array of taxonomies. 
  380. */ 
  381. $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); 
  382.  
  383. if ( ! empty( $exclusions ) ) { 
  384. // Must do string manipulation here for backward compatibility with filter. 
  385. $this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions ); 
  386.  
  387. if ( ! empty( $args['name'] ) ) { 
  388. $names = (array) $args['name']; 
  389. foreach ( $names as &$_name ) { 
  390. // `sanitize_term_field()` returns slashed data. 
  391. $_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) ); 
  392.  
  393. $this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')"; 
  394.  
  395. if ( ! empty( $args['slug'] ) ) { 
  396. if ( is_array( $args['slug'] ) ) { 
  397. $slug = array_map( 'sanitize_title', $args['slug'] ); 
  398. $this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')"; 
  399. } else { 
  400. $slug = sanitize_title( $args['slug'] ); 
  401. $this->sql_clauses['where']['slug'] = "t.slug = '$slug'"; 
  402.  
  403. if ( ! empty( $args['term_taxonomy_id'] ) ) { 
  404. if ( is_array( $args['term_taxonomy_id'] ) ) { 
  405. $tt_ids = implode( ', ', array_map( 'intval', $args['term_taxonomy_id'] ) ); 
  406. $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; 
  407. } else { 
  408. $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] ); 
  409.  
  410. if ( ! empty( $args['name__like'] ) ) { 
  411. $this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' ); 
  412.  
  413. if ( ! empty( $args['description__like'] ) ) { 
  414. $this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' ); 
  415.  
  416. if ( '' !== $parent ) { 
  417. $parent = (int) $parent; 
  418. $this->sql_clauses['where']['parent'] = "tt.parent = '$parent'"; 
  419.  
  420. $hierarchical = $args['hierarchical']; 
  421. if ( 'count' == $args['fields'] ) { 
  422. $hierarchical = false; 
  423. if ( $args['hide_empty'] && !$hierarchical ) { 
  424. $this->sql_clauses['where']['count'] = 'tt.count > 0'; 
  425.  
  426. $number = $args['number']; 
  427. $offset = $args['offset']; 
  428.  
  429. // Don't limit the query results when we have to descend the family tree. 
  430. if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { 
  431. if ( $offset ) { 
  432. $limits = 'LIMIT ' . $offset . ', ' . $number; 
  433. } else { 
  434. $limits = 'LIMIT ' . $number; 
  435. } else { 
  436. $limits = ''; 
  437.  
  438.  
  439. if ( ! empty( $args['search'] ) ) { 
  440. $this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] ); 
  441.  
  442. // Meta query support. 
  443. $join = ''; 
  444. $distinct = ''; 
  445.  
  446. // Reparse meta_query query_vars, in case they were modified in a 'pre_get_terms' callback. 
  447. $this->meta_query->parse_query_vars( $this->query_vars ); 
  448. $mq_sql = $this->meta_query->get_sql( 'term', 't', 'term_id' ); 
  449. $meta_clauses = $this->meta_query->get_clauses(); 
  450.  
  451. if ( ! empty( $meta_clauses ) ) { 
  452. $join .= $mq_sql['join']; 
  453. $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] ); 
  454. $distinct .= "DISTINCT"; 
  455.  
  456.  
  457. $selects = array(); 
  458. switch ( $args['fields'] ) { 
  459. case 'all': 
  460. $selects = array( 't.*', 'tt.*' ); 
  461. break; 
  462. case 'ids': 
  463. case 'id=>parent': 
  464. $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' ); 
  465. break; 
  466. case 'names': 
  467. $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' ); 
  468. break; 
  469. case 'count': 
  470. $orderby = ''; 
  471. $order = ''; 
  472. $selects = array( 'COUNT(*)' ); 
  473. break; 
  474. case 'id=>name': 
  475. $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' ); 
  476. break; 
  477. case 'id=>slug': 
  478. $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' ); 
  479. break; 
  480.  
  481. $_fields = $args['fields']; 
  482.  
  483. /** 
  484. * Filters the fields to select in the terms query. 
  485. * Field lists modified using this filter will only modify the term fields returned 
  486. * by the function when the `$fields` parameter set to 'count' or 'all'. In all other 
  487. * cases, the term fields in the results array will be determined by the `$fields` 
  488. * parameter alone. 
  489. * Use of this filter can result in unpredictable behavior, and is not recommended. 
  490. * @since 2.8.0 
  491. * @param array $selects An array of fields to select for the terms query. 
  492. * @param array $args An array of term query arguments. 
  493. * @param array $taxonomies An array of taxonomies. 
  494. */ 
  495. $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); 
  496.  
  497. $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 
  498.  
  499. $where = implode( ' AND ', $this->sql_clauses['where'] ); 
  500.  
  501. $pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ); 
  502.  
  503. /** 
  504. * Filters the terms query SQL clauses. 
  505. * @since 3.1.0 
  506. * @param array $pieces Terms query SQL clauses. 
  507. * @param array $taxonomies An array of taxonomies. 
  508. * @param array $args An array of terms query arguments. 
  509. */ 
  510. $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); 
  511.  
  512. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 
  513. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 
  514. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 
  515. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; 
  516. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 
  517. $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; 
  518. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 
  519.  
  520. if ( $where ) { 
  521. $where = "WHERE $where"; 
  522.  
  523. $this->sql_clauses['select'] = "SELECT $distinct $fields"; 
  524. $this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join"; 
  525. $this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : ''; 
  526. $this->sql_clauses['limits'] = $limits; 
  527.  
  528. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 
  529.  
  530. // $args can be anything. Only use the args defined in defaults to compute the key. 
  531. $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); 
  532. $last_changed = wp_cache_get( 'last_changed', 'terms' ); 
  533. if ( ! $last_changed ) { 
  534. $last_changed = microtime(); 
  535. wp_cache_set( 'last_changed', $last_changed, 'terms' ); 
  536. $cache_key = "get_terms:$key:$last_changed"; 
  537. $cache = wp_cache_get( $cache_key, 'terms' ); 
  538. if ( false !== $cache ) { 
  539. if ( 'all' === $_fields ) { 
  540. $cache = array_map( 'get_term', $cache ); 
  541.  
  542. $this->terms = $cache; 
  543. return $this->terms; 
  544.  
  545. if ( 'count' == $_fields ) { 
  546. return $wpdb->get_var( $this->request ); 
  547.  
  548. $terms = $wpdb->get_results( $this->request ); 
  549. if ( 'all' == $_fields ) { 
  550. update_term_cache( $terms ); 
  551.  
  552. // Prime termmeta cache. 
  553. if ( $args['update_term_meta_cache'] ) { 
  554. $term_ids = wp_list_pluck( $terms, 'term_id' ); 
  555. update_termmeta_cache( $term_ids ); 
  556.  
  557. if ( empty( $terms ) ) { 
  558. wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); 
  559. return array(); 
  560.  
  561. if ( $child_of ) { 
  562. foreach ( $taxonomies as $_tax ) { 
  563. $children = _get_term_hierarchy( $_tax ); 
  564. if ( ! empty( $children ) ) { 
  565. $terms = _get_term_children( $child_of, $terms, $_tax ); 
  566.  
  567. // Update term counts to include children. 
  568. if ( $args['pad_counts'] && 'all' == $_fields ) { 
  569. foreach ( $taxonomies as $_tax ) { 
  570. _pad_term_counts( $terms, $_tax ); 
  571.  
  572. // Make sure we show empty categories that have children. 
  573. if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { 
  574. foreach ( $terms as $k => $term ) { 
  575. if ( ! $term->count ) { 
  576. $children = get_term_children( $term->term_id, $term->taxonomy ); 
  577. if ( is_array( $children ) ) { 
  578. foreach ( $children as $child_id ) { 
  579. $child = get_term( $child_id, $term->taxonomy ); 
  580. if ( $child->count ) { 
  581. continue 2; 
  582.  
  583. // It really is empty. 
  584. unset( $terms[ $k ] ); 
  585.  
  586. $_terms = array(); 
  587. if ( 'id=>parent' == $_fields ) { 
  588. foreach ( $terms as $term ) { 
  589. $_terms[ $term->term_id ] = $term->parent; 
  590. } elseif ( 'ids' == $_fields ) { 
  591. foreach ( $terms as $term ) { 
  592. $_terms[] = $term->term_id; 
  593. } elseif ( 'names' == $_fields ) { 
  594. foreach ( $terms as $term ) { 
  595. $_terms[] = $term->name; 
  596. } elseif ( 'id=>name' == $_fields ) { 
  597. foreach ( $terms as $term ) { 
  598. $_terms[ $term->term_id ] = $term->name; 
  599. } elseif ( 'id=>slug' == $_fields ) { 
  600. foreach ( $terms as $term ) { 
  601. $_terms[ $term->term_id ] = $term->slug; 
  602.  
  603. if ( ! empty( $_terms ) ) { 
  604. $terms = $_terms; 
  605.  
  606. // Hierarchical queries are not limited, so 'offset' and 'number' must be handled now. 
  607. if ( $hierarchical && $number && is_array( $terms ) ) { 
  608. if ( $offset >= count( $terms ) ) { 
  609. $terms = array(); 
  610. } else { 
  611. $terms = array_slice( $terms, $offset, $number, true ); 
  612.  
  613. wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); 
  614.  
  615. if ( 'all' === $_fields ) { 
  616. $terms = array_map( 'get_term', $terms ); 
  617.  
  618. $this->terms = $terms; 
  619. return $this->terms; 
  620.  
  621. /** 
  622. * Parse and sanitize 'orderby' keys passed to the term query. 
  623. * @since 4.6.0 
  624. * @access protected 
  625. * @global wpdb $wpdb WordPress database abstraction object. 
  626. * @param string $orderby_raw Alias for the field to order by. 
  627. * @return string|false Value to used in the ORDER clause. False otherwise. 
  628. */ 
  629. protected function parse_orderby( $orderby_raw ) { 
  630. $_orderby = strtolower( $orderby_raw ); 
  631. $maybe_orderby_meta = false; 
  632. if ( 'count' == $_orderby ) { 
  633. $orderby = 'tt.count'; 
  634. } elseif ( 'name' == $_orderby ) { 
  635. $orderby = 't.name'; 
  636. } elseif ( 'slug' == $_orderby ) { 
  637. $orderby = 't.slug'; 
  638. } elseif ( 'include' == $_orderby && ! empty( $this->query_vars['include'] ) ) { 
  639. $include = implode( ', ', array_map( 'absint', $this->query_vars['include'] ) ); 
  640. $orderby = "FIELD( t.term_id, $include )"; 
  641. } elseif ( 'term_group' == $_orderby ) { 
  642. $orderby = 't.term_group'; 
  643. } elseif ( 'description' == $_orderby ) { 
  644. $orderby = 'tt.description'; 
  645. } elseif ( 'none' == $_orderby ) { 
  646. $orderby = ''; 
  647. } elseif ( empty( $_orderby ) || 'id' == $_orderby || 'term_id' === $_orderby ) { 
  648. $orderby = 't.term_id'; 
  649. } else { 
  650. $orderby = 't.name'; 
  651.  
  652. // This may be a value of orderby related to meta. 
  653. $maybe_orderby_meta = true; 
  654.  
  655. /** 
  656. * Filters the ORDERBY clause of the terms query. 
  657. * @since 2.8.0 
  658. * @param string $orderby `ORDERBY` clause of the terms query. 
  659. * @param array $args An array of terms query arguments. 
  660. * @param array $taxonomies An array of taxonomies. 
  661. */ 
  662. $orderby = apply_filters( 'get_terms_orderby', $orderby, $this->query_vars, $this->query_vars['taxonomy'] ); 
  663.  
  664. // Run after the 'get_terms_orderby' filter for backward compatibility. 
  665. if ( $maybe_orderby_meta ) { 
  666. $maybe_orderby_meta = $this->parse_orderby_meta( $_orderby ); 
  667. if ( $maybe_orderby_meta ) { 
  668. $orderby = $maybe_orderby_meta; 
  669.  
  670. return $orderby; 
  671.  
  672. /** 
  673. * Generate the ORDER BY clause for an 'orderby' param that is potentially related to a meta query. 
  674. * @since 4.6.0 
  675. * @access public 
  676. * @param string $orderby_raw Raw 'orderby' value passed to WP_Term_Query. 
  677. * @return string 
  678. */ 
  679. protected function parse_orderby_meta( $orderby_raw ) { 
  680. $orderby = ''; 
  681.  
  682. // Tell the meta query to generate its SQL, so we have access to table aliases. 
  683. $this->meta_query->get_sql( 'term', 't', 'term_id' ); 
  684. $meta_clauses = $this->meta_query->get_clauses(); 
  685. if ( ! $meta_clauses || ! $orderby_raw ) { 
  686. return $orderby; 
  687.  
  688. $allowed_keys = array(); 
  689. $primary_meta_key = null; 
  690. $primary_meta_query = reset( $meta_clauses ); 
  691. if ( ! empty( $primary_meta_query['key'] ) ) { 
  692. $primary_meta_key = $primary_meta_query['key']; 
  693. $allowed_keys[] = $primary_meta_key; 
  694. $allowed_keys[] = 'meta_value'; 
  695. $allowed_keys[] = 'meta_value_num'; 
  696. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); 
  697.  
  698. if ( ! in_array( $orderby_raw, $allowed_keys, true ) ) { 
  699. return $orderby; 
  700.  
  701. switch( $orderby_raw ) { 
  702. case $primary_meta_key: 
  703. case 'meta_value': 
  704. if ( ! empty( $primary_meta_query['type'] ) ) { 
  705. $orderby = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; 
  706. } else { 
  707. $orderby = "{$primary_meta_query['alias']}.meta_value"; 
  708. break; 
  709.  
  710. case 'meta_value_num': 
  711. $orderby = "{$primary_meta_query['alias']}.meta_value+0"; 
  712. break; 
  713.  
  714. default: 
  715. if ( array_key_exists( $orderby_raw, $meta_clauses ) ) { 
  716. // $orderby corresponds to a meta_query clause. 
  717. $meta_clause = $meta_clauses[ $orderby_raw ]; 
  718. $orderby = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; 
  719. break; 
  720.  
  721. return $orderby; 
  722.  
  723. /** 
  724. * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 
  725. * @since 4.6.0 
  726. * @access protected 
  727. * @param string $order The 'order' query variable. 
  728. * @return string The sanitized 'order' query variable. 
  729. */ 
  730. protected function parse_order( $order ) { 
  731. if ( ! is_string( $order ) || empty( $order ) ) { 
  732. return 'DESC'; 
  733.  
  734. if ( 'ASC' === strtoupper( $order ) ) { 
  735. return 'ASC'; 
  736. } else { 
  737. return 'DESC'; 
  738.  
  739. /** 
  740. * Used internally to generate a SQL string related to the 'search' parameter. 
  741. * @since 4.6.0 
  742. * @access protected 
  743. * @global wpdb $wpdb WordPress database abstraction object. 
  744. * @param string $string 
  745. * @return string 
  746. */ 
  747. protected function get_search_sql( $string ) { 
  748. global $wpdb; 
  749.  
  750. $like = '%' . $wpdb->esc_like( $string ) . '%'; 
  751.  
  752. return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );