Make WordPress Core

Changeset 16267


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

Make get_tax_sql() a standalone function. See #15032

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-object-query.php

    r16266 r16267  
    7373
    7474    /*
    75      * Used internally to generate an SQL string for searching across multiple taxonomies
    76      *
    77      * @access protected
    78      * @since 3.1.0
    79      *
    80      * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array:
    81      * - 'taxonomy' string|array The taxonomy being queried
    82      * - 'terms' string|array The list of terms
    83      * - 'field' string (optional) Which term field is being used.
    84      *      Possible values: 'term_id', 'slug' or 'name'
    85      *      Default: 'slug'
    86      * - 'operator' string (optional)
    87      *      Possible values: 'IN' and 'NOT IN'.
    88      *      Default: 'IN'
    89      * - 'include_children' bool (optional) Whether to include child terms.
    90      *      Default: true
    91      *
    92      * @param string $object_id_column
    93      * @return string
    94      */
    95     function get_tax_sql( $tax_query, $object_id_column ) {
    96         global $wpdb;
    97 
    98         $sql = array();
    99         foreach ( $tax_query as $query ) {
    100             if ( !isset( $query['include_children'] ) )
    101                 $query['include_children'] = true;
    102 
    103             $query['do_query'] = false;
    104 
    105             $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query );
    106 
    107             if ( empty( $sql_single ) )
    108                 return ' AND 0 = 1';
    109 
    110             $sql[] = $sql_single;
    111         }
    112 
    113         if ( 1 == count( $sql ) ) {
    114             $ids = $wpdb->get_col( $sql[0] );
    115         } else {
    116             $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1";
    117             foreach ( $sql as $query )
    118                 $r .= " AND object_id IN ($query)";
    119 
    120             $ids = $wpdb->get_col( $r );
    121         }
    122 
    123         if ( !empty( $ids ) )
    124             return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")";
    125         else
    126             return ' AND 0 = 1';
    127     }
    128 
    129     /*
    13075     * Used internally to generate an SQL string for searching across multiple columns
    13176     *
  • trunk/wp-includes/query.php

    r16266 r16267  
    19001900            }
    19011901
    1902             $where .= $this->get_tax_sql( $q['tax_query'], "$wpdb->posts.ID" );
     1902            $where .= get_tax_sql( $q['tax_query'], "$wpdb->posts.ID" );
    19031903
    19041904            // Back-compat
  • 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.