Introduce 'relation' operator between tax queries. Props Otto42 for initial patch. See #12891
git-svn-id: http://svn.automattic.com/wordpress/trunk@16555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b43ad21bc2
commit
719b8d8f4a
|
@ -524,9 +524,19 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$join = '';
|
$join = '';
|
||||||
$where = '';
|
$where = array();
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
||||||
|
if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
|
||||||
|
$relation = 'OR';
|
||||||
|
} else {
|
||||||
|
$relation = 'AND';
|
||||||
|
}
|
||||||
|
|
||||||
foreach ( $tax_query as $query ) {
|
foreach ( $tax_query as $query ) {
|
||||||
|
if ( ! is_array( $query ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
extract( wp_parse_args( $query, array(
|
extract( wp_parse_args( $query, array(
|
||||||
'taxonomy' => array(),
|
'taxonomy' => array(),
|
||||||
'terms' => array(),
|
'terms' => array(),
|
||||||
|
@ -566,8 +576,13 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'IN' == $operator ) {
|
if ( 'IN' == $operator ) {
|
||||||
if ( empty( $terms ) )
|
|
||||||
|
if ( empty( $terms ) ) {
|
||||||
|
if ( 'OR' == $relation )
|
||||||
|
continue;
|
||||||
|
else
|
||||||
return array( 'join' => '', 'where' => ' AND 0 = 1' );
|
return array( 'join' => '', 'where' => ' AND 0 = 1' );
|
||||||
|
}
|
||||||
|
|
||||||
$terms = implode( ',', $terms );
|
$terms = implode( ',', $terms );
|
||||||
|
|
||||||
|
@ -577,24 +592,30 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
$join .= $i ? " AS $alias" : '';
|
$join .= $i ? " AS $alias" : '';
|
||||||
$join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
|
$join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
|
||||||
|
|
||||||
$where .= " AND $alias.term_taxonomy_id $operator ($terms)";
|
$where[] = "$alias.term_taxonomy_id $operator ($terms)";
|
||||||
|
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
elseif ( 'NOT IN' == $operator ) {
|
elseif ( 'NOT IN' == $operator ) {
|
||||||
|
|
||||||
if ( empty( $terms ) )
|
if ( empty( $terms ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$terms = implode( ',', $terms );
|
$terms = implode( ',', $terms );
|
||||||
|
|
||||||
$where .= " AND $primary_table.$primary_id_column NOT IN (
|
$where[] = "$primary_table.$primary_id_column NOT IN (
|
||||||
SELECT object_id
|
SELECT object_id
|
||||||
FROM $wpdb->term_relationships
|
FROM $wpdb->term_relationships
|
||||||
WHERE term_taxonomy_id IN ($terms)
|
WHERE term_taxonomy_id IN ($terms)
|
||||||
)";
|
)";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !empty( $where ) )
|
||||||
|
$where = ' AND ( ' . implode( " $relation ", $where ) . ' )';
|
||||||
|
else
|
||||||
|
$where = '';
|
||||||
|
|
||||||
return compact( 'join', 'where' );
|
return compact( 'join', 'where' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue