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