92 * @param string|array $query { |
92 * @param string|array $query { |
93 * Optional. Array or query string of term query parameters. Default empty. |
93 * Optional. Array or query string of term query parameters. Default empty. |
94 * |
94 * |
95 * @type string|array $taxonomy Taxonomy name, or array of taxonomies, to which results should |
95 * @type string|array $taxonomy Taxonomy name, or array of taxonomies, to which results should |
96 * be limited. |
96 * be limited. |
97 * @type int|array $object_ids Optional. Object ID, or array of object IDs. Results will be |
97 * @type int|int[] $object_ids Optional. Object ID, or array of object IDs. Results will be |
98 * limited to terms associated with these objects. |
98 * limited to terms associated with these objects. |
99 * @type string $orderby Field(s) to order terms by. Accepts: |
99 * @type string $orderby Field(s) to order terms by. Accepts: |
100 * - term fields ('name', 'slug', 'term_group', 'term_id', 'id', |
100 * * term fields ('name', 'slug', 'term_group', 'term_id', 'id', |
101 * 'description', 'parent', 'term_order'). Unless `$object_ids` |
101 * 'description', 'parent', 'term_order'). Unless `$object_ids` |
102 * is not empty, 'term_order' is treated the same as 'term_id'. |
102 * is not empty, 'term_order' is treated the same as 'term_id'. |
103 * - 'count' for term taxonomy count. |
103 * * 'count' to use the number of objects associated with the term. |
104 * - 'include' to match the 'order' of the $include param. |
104 * * 'include' to match the 'order' of the $include param. |
105 * - 'slug__in' to match the 'order' of the $slug param. |
105 * * 'slug__in' to match the 'order' of the $slug param. |
106 * - 'meta_value', 'meta_value_num'. |
106 * * 'meta_value', 'meta_value_num'. |
107 * - the value of `$meta_key`. |
107 * the value of `$meta_key`. |
108 * - the array keys of `$meta_query`. |
108 * the array keys of `$meta_query`. |
109 * - 'none' to omit the ORDER BY clause. |
109 * * 'none' to omit the ORDER BY clause. |
110 * Defaults to 'name'. |
110 * Defaults to 'name'. |
111 * @type string $order Whether to order terms in ascending or descending order. |
111 * @type string $order Whether to order terms in ascending or descending order. |
112 * Accepts 'ASC' (ascending) or 'DESC' (descending). |
112 * Accepts 'ASC' (ascending) or 'DESC' (descending). |
113 * Default 'ASC'. |
113 * Default 'ASC'. |
114 * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts |
114 * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts |
115 * 1|true or 0|false. Default 1|true. |
115 * 1|true or 0|false. Default 1|true. |
116 * @type array|string $include Array or comma/space-separated string of term IDs to include. |
116 * @type int[]|string $include Array or comma/space-separated string of term IDs to include. |
117 * Default empty array. |
117 * Default empty array. |
118 * @type array|string $exclude Array or comma/space-separated string of term IDs to exclude. |
118 * @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. |
119 * If $include is non-empty, $exclude is ignored. |
119 * If $include is non-empty, $exclude is ignored. |
120 * Default empty array. |
120 * Default empty array. |
121 * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude |
121 * @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude |
122 * along with all of their descendant terms. If $include is |
122 * along with all of their descendant terms. If $include is |
123 * non-empty, $exclude_tree is ignored. Default empty array. |
123 * non-empty, $exclude_tree is ignored. Default empty array. |
124 * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any |
124 * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any |
125 * positive number. Default ''|0 (all). Note that $number may |
125 * positive number. Default ''|0 (all). Note that $number may |
126 * not return accurate results when coupled with $object_ids. |
126 * not return accurate results when coupled with $object_ids. |
127 * See #41796 for details. |
127 * See #41796 for details. |
128 * @type int $offset The number by which to offset the terms query. Default empty. |
128 * @type int $offset The number by which to offset the terms query. Default empty. |
129 * @type string $fields Term fields to query for. Accepts: |
129 * @type string $fields Term fields to query for. Accepts: |
130 * - 'all' Returns an array of complete term objects (`WP_Term[]`). |
130 * * 'all' Returns an array of complete term objects (`WP_Term[]`). |
131 * - 'all_with_object_id' Returns an array of term objects |
131 * * 'all_with_object_id' Returns an array of term objects |
132 * with the 'object_id' param (`WP_Term[]`). Works only |
132 * with the 'object_id' param (`WP_Term[]`). Works only |
133 * when the `$object_ids` parameter is populated. |
133 * when the `$object_ids` parameter is populated. |
134 * - 'ids' Returns an array of term IDs (`int[]`). |
134 * * 'ids' Returns an array of term IDs (`int[]`). |
135 * - 'tt_ids' Returns an array of term taxonomy IDs (`int[]`). |
135 * * 'tt_ids' Returns an array of term taxonomy IDs (`int[]`). |
136 * - 'names' Returns an array of term names (`string[]`). |
136 * * 'names' Returns an array of term names (`string[]`). |
137 * - 'slugs' Returns an array of term slugs (`string[]`). |
137 * * 'slugs' Returns an array of term slugs (`string[]`). |
138 * - 'count' Returns the number of matching terms (`int`). |
138 * * 'count' Returns the number of matching terms (`int`). |
139 * - 'id=>parent' Returns an associative array of parent term IDs, |
139 * * 'id=>parent' Returns an associative array of parent term IDs, |
140 * keyed by term ID (`int[]`). |
140 * keyed by term ID (`int[]`). |
141 * - 'id=>name' Returns an associative array of term names, |
141 * * 'id=>name' Returns an associative array of term names, |
142 * keyed by term ID (`string[]`). |
142 * keyed by term ID (`string[]`). |
143 * - 'id=>slug' Returns an associative array of term slugs, |
143 * * 'id=>slug' Returns an associative array of term slugs, |
144 * keyed by term ID (`string[]`). |
144 * keyed by term ID (`string[]`). |
145 * Default 'all'. |
145 * Default 'all'. |
146 * @type bool $count Whether to return a term count. If true, will take precedence |
146 * @type bool $count Whether to return a term count. If true, will take precedence |
147 * over `$fields`. Default false. |
147 * over `$fields`. Default false. |
148 * @type string|array $name Optional. Name or array of names to return term(s) for. |
148 * @type string|array $name Optional. Name or array of names to return term(s) for. |
149 * Default empty. |
149 * Default empty. |
150 * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. |
150 * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. |
151 * Default empty. |
151 * Default empty. |
152 * @type int|array $term_taxonomy_id Optional. Term taxonomy ID, or array of term taxonomy IDs, |
152 * @type int|int[] $term_taxonomy_id Optional. Term taxonomy ID, or array of term taxonomy IDs, |
153 * to match when querying terms. |
153 * to match when querying terms. |
154 * @type bool $hierarchical Whether to include terms that have non-empty descendants |
154 * @type bool $hierarchical Whether to include terms that have non-empty descendants |
155 * (even if $hide_empty is set to true). Default true. |
155 * (even if $hide_empty is set to true). Default true. |
156 * @type string $search Search criteria to match terms. Will be SQL-formatted with |
156 * @type string $search Search criteria to match terms. Will be SQL-formatted with |
157 * wildcards before and after. Default empty. |
157 * wildcards before and after. Default empty. |
283 */ |
283 */ |
284 do_action( 'parse_term_query', $this ); |
284 do_action( 'parse_term_query', $this ); |
285 } |
285 } |
286 |
286 |
287 /** |
287 /** |
288 * Sets up the query for retrieving terms. |
288 * Sets up the query and retrieves the results. |
|
289 * |
|
290 * The return type varies depending on the value passed to `$args['fields']`. See |
|
291 * WP_Term_Query::get_terms() for details. |
289 * |
292 * |
290 * @since 4.6.0 |
293 * @since 4.6.0 |
291 * |
294 * |
292 * @param string|array $query Array or URL query string of parameters. |
295 * @param string|array $query Array or URL query string of parameters. |
293 * @return array|int List of terms, or number of terms when 'count' is passed as a query var. |
296 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string |
|
297 * when 'count' is passed as a query var. |
294 */ |
298 */ |
295 public function query( $query ) { |
299 public function query( $query ) { |
296 $this->query_vars = wp_parse_args( $query ); |
300 $this->query_vars = wp_parse_args( $query ); |
297 return $this->get_terms(); |
301 return $this->get_terms(); |
298 } |
302 } |
299 |
303 |
300 /** |
304 /** |
301 * Get terms, based on query_vars. |
305 * Retrieves the query results. |
|
306 * |
|
307 * The return type varies depending on the value passed to `$args['fields']`. |
|
308 * |
|
309 * The following will result in an array of `WP_Term` objects being returned: |
|
310 * |
|
311 * - 'all' |
|
312 * - 'all_with_object_id' |
|
313 * |
|
314 * The following will result in a numeric string being returned: |
|
315 * |
|
316 * - 'count' |
|
317 * |
|
318 * The following will result in an array of text strings being returned: |
|
319 * |
|
320 * - 'id=>name' |
|
321 * - 'id=>slug' |
|
322 * - 'names' |
|
323 * - 'slugs' |
|
324 * |
|
325 * The following will result in an array of numeric strings being returned: |
|
326 * |
|
327 * - 'id=>parent' |
|
328 * |
|
329 * The following will result in an array of integers being returned: |
|
330 * |
|
331 * - 'ids' |
|
332 * - 'tt_ids' |
302 * |
333 * |
303 * @since 4.6.0 |
334 * @since 4.6.0 |
304 * |
335 * |
305 * @global wpdb $wpdb WordPress database abstraction object. |
336 * @global wpdb $wpdb WordPress database abstraction object. |
306 * |
337 * |
307 * @return array List of terms. |
338 * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string |
|
339 * when 'count' is passed as a query var. |
308 */ |
340 */ |
309 public function get_terms() { |
341 public function get_terms() { |
310 global $wpdb; |
342 global $wpdb; |
311 |
343 |
312 $this->parse_query( $this->query_vars ); |
344 $this->parse_query( $this->query_vars ); |