Title: WP_Tax_Query::find_compatible_table_alias
Published: December 18, 2014
Last modified: May 20, 2026

---

# WP_Tax_Query::find_compatible_table_alias( array $clause, array $parent_query ): string|false

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#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](https://developer.wordpress.org/reference/classes/wp_tax_query/),
this only applies to ‘IN’ clauses that are connected by the relation ‘OR’.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#parameters)󠁿

 `$clause`arrayrequired

Query clause.

`$parent_query`arrayrequired

Parent query of $clause.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#return)󠁿

 string|false Table alias if found, otherwise false.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#source)󠁿

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

    	// Confidence 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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-tax-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wp-tax-query.php#L505)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-tax-query.php#L505-L537)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Tax_Query::is_first_order_clause()](https://developer.wordpress.org/reference/classes/wp_tax_query/is_first_order_clause/)`wp-includes/class-wp-tax-query.php` |

Determines whether a clause is first-order.

  |

| Used by | Description | 
| [WP_Tax_Query::get_sql_for_clause()](https://developer.wordpress.org/reference/classes/wp_tax_query/get_sql_for_clause/)`wp-includes/class-wp-tax-query.php` |

Generates SQL JOIN and WHERE clauses for a “first-order” query clause.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_tax_query/find_compatible_table_alias/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.1.0](https://developer.wordpress.org/reference/since/4.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_tax_query%2Ffind_compatible_table_alias%2F)
before being able to contribute a note or feedback.