Make WordPress Core

Changeset 17251


Ignore:
Timestamp:
01/11/2011 06:56:59 PM (14 years ago)
Author:
ryan
Message:

Set tag_id for tag queries. Add NOT support to wp_list_filter(). Props scribu. fixes #16170

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r17032 r17251  
    30503050 * @param array $list An array of objects to filter
    30513051 * @param array $args An array of key => value arguments to match against each object
    3052  * @param string $operator The logical operation to perform. 'or' means only one element
    3053  *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
     3052 * @param string $operator The logical operation to perform:
     3053 *    'AND' means all elements from the array must match;
     3054 *    'OR' means only one element needs to match;
     3055 *    'NOT' means no elements may match.
     3056 *   The default is 'AND'.
    30543057 * @return array
    30553058 */
    3056 function wp_list_filter( $list, $args = array(), $operator = 'and' ) {
     3059function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
    30573060    if ( empty( $args ) )
    30583061        return $list;
    30593062
     3063    $operator = strtoupper( $operator );
    30603064    $count = count( $args );
    3061 
    30623065    $filtered = array();
    30633066
    30643067    foreach ( $list as $key => $obj ) {
    30653068        $matched = count( array_intersect_assoc( (array) $obj, $args ) );
    3066         if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) {
     3069        if ( ( 'AND' == $operator && $matched == $count )
     3070          || ( 'OR' == $operator && $matched <= $count )
     3071          || ( 'NOT' == $operator && 0 == $matched ) ) {
    30673072            $filtered[$key] = $obj;
    30683073        }
  • trunk/wp-includes/query.php

    r17246 r17251  
    21442144        // Back-compat
    21452145        if ( !empty($this->tax_query->queries) ) {
    2146             $tax_query_in = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'IN' ) );
    2147             if ( !empty( $tax_query_in ) ) {
     2146            $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
     2147            if ( !empty( $tax_query_in_and ) ) {
    21482148                if ( !isset( $q['taxonomy'] ) ) {
    2149                     foreach ( $tax_query_in as $a_tax_query ) {
     2149                    foreach ( $tax_query_in_and as $a_tax_query ) {
    21502150                        if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
    21512151                            $q['taxonomy'] = $a_tax_query['taxonomy'];
     
    21602160                }
    21612161
    2162                 $cat_query = wp_list_filter( $tax_query_in, array( 'taxonomy' => 'category' ) );
     2162                $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
    21632163                if ( !empty( $cat_query ) ) {
    21642164                    $cat_query = reset( $cat_query );
    2165                     $cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
    2166                     if ( $cat ) {
    2167                         $this->set( 'cat', $cat->term_id );
    2168                         $this->set( 'category_name', $cat->slug );
     2165                    $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
     2166                    if ( $the_cat ) {
     2167                        $this->set( 'cat', $the_cat->term_id );
     2168                        $this->set( 'category_name', $the_cat->slug );
    21692169                    }
     2170                    unset( $the_cat );
    21702171                }
     2172                unset( $cat_query );
     2173
     2174                $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
     2175                if ( !empty( $tag_query ) ) {
     2176                    $tag_query = reset( $tag_query );
     2177                    $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
     2178                    if ( $the_tag ) {
     2179                        $this->set( 'tag_id', $the_tag->term_id );
     2180                    }
     2181                    unset( $the_tag );
     2182                }
     2183                unset( $tag_query );
    21712184            }
    21722185        }
     
    28452858
    28462859        if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    2847             $tax_query_in = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'IN' ) );
    2848 
    2849             $query = reset( $tax_query_in );
     2860            $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
     2861
     2862            $query = reset( $tax_query_in_and );
    28502863
    28512864            if ( 'term_id' == $query['field'] )
Note: See TracChangeset for help on using the changeset viewer.