get_tax_sql( array $tax_query, string $primary_table, string $primary_id_column ): string[]

Given a taxonomy query, generates SQL to be appended to a main query.

Description

See also

Parameters

$tax_queryarrayrequired
A compact tax query
$primary_tablestringrequired
$primary_id_columnstringrequired

Return

string[]

Source

function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
	$tax_query_obj = new WP_Tax_Query( $tax_query );
	return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
}

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Get SQL query part for taxonomy

    $tax_query = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'cat-a', 'cat-b' ),
        )
    );
    
    global $wpdb;
    $tax_sql = get_tax_sql( $tax_query, $wpdb->posts, 'ID' );

    Output would be something like:

    Array
    (
        [join] =>  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
        [where] =>  AND ( wp_term_relationships.term_taxonomy_id IN (3,4,10,19,25,95) )
    )

You must log in before being able to contribute a note or feedback.