暂无描述

class-wp-term-query.php 36KB

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