Make WordPress Core


Ignore:
Timestamp:
10/06/2010 10:40:30 AM (14 years ago)
Author:
scribu
Message:

Generalize taxonomy queries:

  • transform wp_tax_query() into WP_Object_Query::get_tax_sql()
  • create parse_tax_query() method in WP_Query
  • add doc-block for $tax_query and $meta_query

See #15032. See #12891.

File:
1 edited

Legend:

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

    r15730 r15731  
    536536
    537537    /**
    538      * Metadata query
     538     * List of metadata queries
     539     *
     540     * A query is an associative array:
     541     * - 'key' string The meta key
     542     * - 'value' string|array The meta value
     543     * - 'compare' (optional) string How to compare the key to the value.
     544     *      Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'IN', 'BETWEEN'.
     545     *      Default: '='
     546     * - 'type' string (optional) The type of the value.
     547     *      Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
     548     *      Default: 'CHAR'
    539549     *
    540550     * @since 3.1.0
     
    543553     */
    544554    var $meta_query = array();
     555
     556    /*
     557     * List of taxonomy queries
     558     *
     559     * A query is an associative array:
     560     * - 'taxonomy' string|array The taxonomy being queried
     561     * - 'terms' string|array The list of terms
     562     * - 'field' string (optional) Which term field is being used.
     563     *      Possible values: 'term_id', 'slug' or 'name'
     564     *      Default: 'slug'
     565     * - 'operator' string (optional)
     566     *      Possible values: 'IN' and 'NOT IN'.
     567     *      Default: 'IN'
     568     * - 'include_children' bool (optional) Wether to include child terms.
     569     *      Default: true
     570     *
     571     * @since 3.1.0
     572     * @access public
     573     * @var array
     574     */
     575    var $tax_query = array();
    545576
    546577    /*
     
    573604     * @access protected
    574605     * @since 3.1.0
     606     *
     607     * @uses $this->meta_query
    575608     *
    576609     * @param string $primary_table
     
    644677
    645678        return array( $join, $where );
     679    }
     680
     681    /*
     682     * Used internally to generate an SQL string for searching across multiple taxonomies
     683     *
     684     * @access protected
     685     * @since 3.1.0
     686     *
     687     * @uses $this->tax_query
     688     *
     689     * @param string $object_id_column
     690     * @return string
     691     */
     692    function get_tax_sql( $object_id_column ) {
     693        global $wpdb;
     694
     695        $sql = array();
     696        foreach ( $this->tax_query as $query ) {
     697            if ( !isset( $query['include_children'] ) )
     698                $query['include_children'] = true;
     699            $query['do_query'] = false;
     700            $sql[] = get_objects_in_term( $query['terms'], $query['taxonomy'], $query );
     701        }
     702
     703        if ( 1 == count( $sql ) ) {
     704            $ids = $wpdb->get_col( $sql[0] );
     705        } else {
     706            $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1";
     707            foreach ( $sql as $query )
     708                $r .= " AND object_id IN ($query)";
     709
     710            $ids = $wpdb->get_col( $r );
     711        }
     712
     713        if ( !empty( $ids ) )
     714            return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")";
     715        else
     716            return ' AND 0 = 1';
    646717    }
    647718
Note: See TracChangeset for help on using the changeset viewer.