Make WordPress Core


Ignore:
Timestamp:
11/09/2010 11:30:35 PM (14 years ago)
Author:
scribu
Message:

Make get_tax_sql() a standalone function. See #15032

File:
1 edited

Legend:

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

    r16251 r16267  
    554554    return $do_query ? $wpdb->get_col( $sql ) : $sql;
    555555}
     556
     557/*
     558 * Given a meta query, generates SQL to be appended to a main query
     559 *
     560 * @since 3.1.0
     561 *
     562 * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array:
     563 * - 'taxonomy' string|array The taxonomy being queried
     564 * - 'terms' string|array The list of terms
     565 * - 'field' string (optional) Which term field is being used.
     566 *      Possible values: 'term_id', 'slug' or 'name'
     567 *      Default: 'slug'
     568 * - 'operator' string (optional)
     569 *      Possible values: 'IN' and 'NOT IN'.
     570 *      Default: 'IN'
     571 * - 'include_children' bool (optional) Whether to include child terms.
     572 *      Default: true
     573 *
     574 * @param string $object_id_column
     575 * @return string
     576 */
     577function get_tax_sql( $tax_query, $object_id_column ) {
     578    global $wpdb;
     579
     580    $sql = array();
     581    foreach ( $tax_query as $query ) {
     582        if ( !isset( $query['include_children'] ) )
     583            $query['include_children'] = true;
     584
     585        $query['do_query'] = false;
     586
     587        $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query );
     588
     589        if ( empty( $sql_single ) )
     590            return ' AND 0 = 1';
     591
     592        $sql[] = $sql_single;
     593    }
     594
     595    if ( 1 == count( $sql ) ) {
     596        $ids = $wpdb->get_col( $sql[0] );
     597    } else {
     598        $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1";
     599        foreach ( $sql as $query )
     600            $r .= " AND object_id IN ($query)";
     601
     602        $ids = $wpdb->get_col( $r );
     603    }
     604
     605    if ( !empty( $ids ) )
     606        return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")";
     607    else
     608        return ' AND 0 = 1';
     609}
     610
    556611
    557612/**
Note: See TracChangeset for help on using the changeset viewer.