/wp-includes/class-wp-term-query.php

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