WP_Tax_Query::transform_query() WordPress Method
The WP_Tax_Query::transform_query() function is used to modify thetax query used to retrieve terms associated with a given object. This function allows you to modify the tax query used to retrieve terms associated with a given object. The tax query is an array of taxonomy query parameters. The first argument is the taxonomy to which the terms should be associated. The second argument is the object ID. You can use this function to modify the tax query used to retrieve terms associated with a given object. The tax query is an array of taxonomy query parameters. The first argument is the taxonomy to which the terms should be associated. The second argument is the object ID. This function is useful for customizing the terms associated with a given object. For example, you could use this function to display only certain terms for a given object.
WP_Tax_Query::transform_query( array $query, string $resulting_field ) #
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
- $query
(array)(Required)The single query. Passed by reference.
- $resulting_field
(string)(Required)The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id', or 'term_id'. Default 'term_id'.
Source
File: wp-includes/class-wp-tax-query.php
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; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
3.2.0 | Introduced. |