WP_Query::parse_tax_query( array $query_vars )
- Since
- 3.1.0
- Source
wp-includes/class-wp-query.php:1176
Description
For BC, this method is not marked as protected. See [28987].
Compatibility
- WordPress
- since 3.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$query_varsarray- The query variables. Passed by reference.
Performance profile
How much work a call to WP_Query::parse_tax_query() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 54–202
- Plugin surface
- 1 hook
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 506.
Third-party callbacks on 'parse_tax_query' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - regexregular expression over the whole input
preg_split()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 36 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::parse_tax_query() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($query_vars) && get_taxonomies() | 54–58 | get_taxonomies(), post_tag(), do_action() |
get_taxonomies() | 66–70 | get_taxonomies(), absint(), post_tag(), do_action() |
empty($query_vars) && get_taxonomies() | 74–77 | get_taxonomies(), preg_split(), post_tag(), do_action() |
get_taxonomies() | 74–80 | get_taxonomies(), array_unique(), array_map(), sort(), post_tag(), do_action() |
empty($query_vars) && get_taxonomies() | 85 | get_taxonomies(), sanitize_term_field(), sort(), post_tag(), do_action() |
get_taxonomies() | 86–89 | get_taxonomies(), preg_split(), absint(), post_tag(), do_action() |
get_taxonomies() | 86–92 | get_taxonomies(), absint(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 94–99 | get_taxonomies(), preg_split(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 95–101 | get_taxonomies(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 97 | get_taxonomies(), sanitize_term_field(), sort(), absint(), post_tag(), do_action() |
get_taxonomies() | 105–107 | get_taxonomies(), sanitize_term_field(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 106–111 | get_taxonomies(), preg_split(), absint(), array_unique(), array_map(), sort(), post_tag(), do_action() |
24 further outcomes, up to 202 instructions
get_taxonomies() | 107–113 | get_taxonomies(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 115–120 | get_taxonomies(), preg_split(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 116–122 | get_taxonomies(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 117–119 | get_taxonomies(), sanitize_term_field(), sort(), absint(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 126–128 | get_taxonomies(), sanitize_term_field(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 127–132 | get_taxonomies(), preg_split(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 128–134 | get_taxonomies(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 136–141 | get_taxonomies(), preg_split(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 137–143 | get_taxonomies(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 138–140 | get_taxonomies(), sanitize_term_field(), sort(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 147–149 | get_taxonomies(), sanitize_term_field(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 148–153 | get_taxonomies(), preg_split(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 149–155 | get_taxonomies(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 157–162 | get_taxonomies(), preg_split(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 159–161 | get_taxonomies(), sanitize_term_field(), sort(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 159–163 | get_taxonomies(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 168–170 | get_taxonomies(), sanitize_term_field(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 169–174 | get_taxonomies(), preg_split(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 171–175 | get_taxonomies(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 179–182 | get_taxonomies(), preg_split(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 180–182 | get_taxonomies(), sanitize_term_field(), sort(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 190 | get_taxonomies(), sanitize_term_field(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 191–193 | get_taxonomies(), preg_split(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
get_taxonomies() | 202 | get_taxonomies(), sanitize_term_field(), sort(), absint(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), array_unique(), array_map(), sort(), post_tag(), do_action() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 506 | 54–202 | 47 | |
| 8.5 | 506 | 54–202 | 47 | |
| 8.4 | 506 | 54–202 | 47 | 21 fewer instructions than PHP 8.3 |
| 8.3 | 527 | 54–208 | 47 | |
| 8.2 | 527 | 54–208 | 47 | |
| 8.1 | 527 | 54–208 | 47 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 528 | 54–209 | 47 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Query::parse_tax_query() runs, in this order:
- do_action( parse_tax_query )actionline 1420 (+244 into the body)
Fires after taxonomy-related query vars have been parsed.
Uses · 7
- get_taxonomies()Retrieves a list of registered taxonomy names or objects.
- wp_basename()i18n-friendly version of basename().
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- absint()Converts a value to non-negative integer.
- sanitize_term_field()Sanitizes the field value in the term based on the context.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Tax_Query::__construct()Constructor.
Used by · 2
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::parse_query()Parses a query string and sets query type booleans.
Source code
public function parse_tax_query( &$query_vars ) { if ( ! empty( $query_vars['tax_query'] ) && is_array( $query_vars['tax_query'] ) ) { $tax_query = $query_vars['tax_query']; } else { $tax_query = array(); } if ( ! empty( $query_vars['taxonomy'] ) && ! empty( $query_vars['term'] ) ) { $tax_query[] = array( 'taxonomy' => $query_vars['taxonomy'], 'terms' => array( $query_vars['term'] ), 'field' => 'slug', ); } foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { if ( 'post_tag' === $taxonomy ) { continue; // Handled further down in the $query_vars['tag'] block. } if ( $t->query_var && ! empty( $query_vars[ $t->query_var ] ) ) { $tax_query_defaults = array( 'taxonomy' => $taxonomy, 'field' => 'slug', ); if ( is_string( $query_vars[ $t->query_var ] ) && ! empty( $t->rewrite['hierarchical'] ) ) { $query_vars[ $t->query_var ] = wp_basename( $query_vars[ $t->query_var ] ); } $term = $query_vars[ $t->query_var ]; if ( ! is_array( $term ) ) { $term = explode( ',', $term ); $term = array_map( 'trim', $term ); } sort( $term ); $term = implode( ',', $term ); if ( str_contains( $term, '+' ) ) { $terms = preg_split( '/[+]+/', $term ); foreach ( $terms as $term ) { $tax_query[] = array_merge( $tax_query_defaults, array( 'terms' => array( $term ), ) ); } } else { $tax_query[] = array_merge( $tax_query_defaults, array( 'terms' => preg_split( '/[,]+/', $term ), ) ); } } } // If query string 'cat' is an array, implode it. if ( is_array( $query_vars['cat'] ) ) { $query_vars['cat'] = implode( ',', $query_vars['cat'] ); } // Category stuff. if ( ! empty( $query_vars['cat'] ) && ! $this->is_singular ) { $cat_in = array(); $cat_not_in = array(); $cat_array = preg_split( '/[,\s]+/', urldecode( $query_vars['cat'] ) ); $cat_array = array_map( 'intval', $cat_array ); sort( $cat_array ); $query_vars['cat'] = implode( ',', $cat_array ); foreach ( $cat_array as $cat ) { if ( $cat > 0 ) { $cat_in[] = $cat; } elseif ( $cat < 0 ) {Changelog
Introduced in 3.1.0. 2 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$query_vars added.verified against source$q removed.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.