WP_Tax_Query::transform_query( array $query, string $resulting_field )
- Since
- 3.2.0
- Source
wp-includes/class-wp-tax-query.php:596
Transforms a single query, from one field to another.
Description
Operates on the
$query object by reference. In the case of error, $query is converted to a WP_Error object.Parameters
$queryarray- The single query. Passed by reference.
$resulting_fieldstring- The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id', or 'term_id'. Default 'term_id'.
Uses · 8
- sanitize_key()Sanitizes a string key.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- WP_Term_Query::__construct()Constructor.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_Tax_Query::clean_query()Validates a single query.
Source
public function transform_query( &$query, $resulting_field ) { if ( empty( $query['terms'] ) ) { return; } if ( $query['field'] === $resulting_field ) { return; } $resulting_field = sanitize_key( $resulting_field ); // Empty 'terms' always results in a null transformation. $terms = array_filter( $query['terms'] ); if ( empty( $terms ) ) { $query['terms'] = array(); $query['field'] = $resulting_field; return; } $args = array( 'get' => 'all', 'number' => 0, 'taxonomy' => $query['taxonomy'], 'update_term_meta_cache' => false, 'orderby' => 'none', ); // Term query parameter name depends on the 'field' being searched on. switch ( $query['field'] ) { case 'slug': $args['slug'] = $terms; break; case 'name': $args['name'] = $terms; break; case 'term_taxonomy_id': $args['term_taxonomy_id'] = $terms; break; default: $args['include'] = wp_parse_id_list( $terms ); break; } if ( ! is_taxonomy_hierarchical( $query['taxonomy'] ) ) { $args['number'] = count( $terms ); } $term_query = new WP_Term_Query(); $term_list = $term_query->query( $args ); if ( is_wp_error( $term_list ) ) { $query = $term_list; return; } if ( 'AND' === $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) { $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) ); return; } $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); $query['field'] = $resulting_field; }History
Introduced in 3.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-tax-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.