WP_Tax_Query::find_compatible_table_alias() WordPress Method

The WP_Tax_Query::find_compatible_table_alias() is a method used to find a compatible table alias for a givenTaxonomy. It is used internally by the WP_Tax_Query class.

WP_Tax_Query::find_compatible_table_alias( array $clause, array $parent_query ) #

Identify an existing table alias that is compatible with the current query clause.


Description

We avoid unnecessary table joins by allowing each clause to look for an existing table alias that is compatible with the query that it needs to perform.

An existing alias is compatible if (a) it is a sibling of $clause (ie, it’s under the scope of the same relation), and (b) the combination of operator and relation between the clauses allows for a shared table join. In the case of WP_Tax_Query, this only applies to ‘IN’ clauses that are connected by the relation ‘OR’.


Top ↑

Parameters

$clause

(array)(Required)Query clause.

$parent_query

(array)(Required)Parent query of $clause.


Top ↑

Return

(string|false) Table alias if found, otherwise false.


Top ↑

Source

File: wp-includes/class-wp-tax-query.php

	protected function find_compatible_table_alias( $clause, $parent_query ) {
		$alias = false;

		// Sanity check. Only IN queries use the JOIN syntax.
		if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) {
			return $alias;
		}

		// Since we're only checking IN queries, we're only concerned with OR relations.
		if ( ! isset( $parent_query['relation'] ) || 'OR' !== $parent_query['relation'] ) {
			return $alias;
		}

		$compatible_operators = array( 'IN' );

		foreach ( $parent_query as $sibling ) {
			if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
				continue;
			}

			if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) {
				continue;
			}

			// The sibling must both have compatible operator to share its alias.
			if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators, true ) ) {
				$alias = preg_replace( '/\W/', '_', $sibling['alias'] );
				break;
			}
		}

		return $alias;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.