WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/23/2010 08:22:27 PM (8 years ago)
Author:
scribu
Message:

Introduce 'relation' operator between tax queries. Props Otto42 for initial patch. See #12891

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r16535 r16555  
    525525
    526526    $join = '';
    527     $where = '';
     527    $where = array();
    528528    $i = 0;
     529
     530    if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
     531        $relation = 'OR';
     532    } else {
     533        $relation = 'AND';
     534    }
     535
    529536    foreach ( $tax_query as $query ) {
     537        if ( ! is_array( $query ) )
     538            continue;
     539
    530540        extract( wp_parse_args( $query, array(
    531541            'taxonomy' => array(),
     
    567577
    568578        if ( 'IN' == $operator ) {
    569             if ( empty( $terms ) )
    570                 return array( 'join' => '', 'where' => ' AND 0 = 1');
     579
     580            if ( empty( $terms ) ) {
     581                if ( 'OR' == $relation )
     582                    continue;
     583                else
     584                    return array( 'join' => '', 'where' => ' AND 0 = 1' );
     585            }
    571586
    572587            $terms = implode( ',', $terms );
     
    578593            $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
    579594
    580             $where .= " AND $alias.term_taxonomy_id $operator ($terms)";
    581 
    582             $i++;
     595            $where[] = "$alias.term_taxonomy_id $operator ($terms)";
    583596        }
    584597        elseif ( 'NOT IN' == $operator ) {
     598
    585599            if ( empty( $terms ) )
    586600                continue;
     
    588602            $terms = implode( ',', $terms );
    589603
    590             $where .= " AND $primary_table.$primary_id_column NOT IN (
    591                 SELECT object_id 
    592                 FROM $wpdb->term_relationships 
     604            $where[] = "$primary_table.$primary_id_column NOT IN (
     605                SELECT object_id
     606                FROM $wpdb->term_relationships
    593607                WHERE term_taxonomy_id IN ($terms)
    594608            )";
    595609        }
    596     }
     610
     611        $i++;
     612    }
     613
     614    if ( !empty( $where ) )
     615        $where = ' AND ( ' . implode( " $relation ", $where ) . ' )';
     616    else
     617        $where = '';
    597618
    598619    return compact( 'join', 'where' );
Note: See TracChangeset for help on using the changeset viewer.