Make WordPress Core


Ignore:
Timestamp:
08/15/2007 10:08:51 PM (17 years ago)
Author:
ryan
Message:

Category union and intersection query vars. fixes #4750

File:
1 edited

Legend:

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

    r5848 r5873  
    414414        }
    415415
     416        $array_keys = array('category__in', 'category__not_in', 'category__and');
     417       
     418        foreach ( $array_keys as $key ) {
     419            if ( !isset($array[$key]))
     420                $array[$key] = array();
     421        }
    416422        return $array;
    417423    }
     
    549555            }
    550556
     557            if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
     558                $qv['category__in'] = array();
     559            } else {
     560                $qv['category__in'] = array_map('intval', $qv['category__in']);
     561                $this->is_category = true; 
     562            }
     563
     564            if ( !is_array($qv['category___not_in']) || empty($qv['category__not_in']) ) {
     565                $qv['category__not_in'] = array();
     566            } else {
     567                $qv['category__not_in'] = array_map('intval', $qv['category__not_in']);
     568            }
     569
     570            if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
     571                $qv['category__and'] = array();
     572            } else {
     573                $qv['category__and'] = array_map('intval', $qv['category__and']);
     574                $this->is_category = true; 
     575            }
     576
    551577            if (  '' != $qv['tag'] )
    552578                $this->is_tag = true;
     
    843869            $q['cat'] = ''.urldecode($q['cat']).'';
    844870            $q['cat'] = addslashes_gpc($q['cat']);
    845             $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
    846871            $cat_array = preg_split('/[,\s]+/', $q['cat']);
    847             $in_cats = $out_cats = array();
    848             $include_cats = $exclude_cats = '';
    849872            foreach ( $cat_array as $cat ) {
    850873                $cat = intval($cat);
     
    852875                $cat = abs($cat);
    853876                if ( $in ) {
    854                     $in_cats[] = $cat;
    855                     $in_cats = array_merge($in_cats, get_term_children($cat, 'category'));
     877                    $q['category__in'][] = $cat;
     878                    $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
    856879                } else {
    857                     $out_cats[] = $cat;
    858                     $out_cats = array_merge($out_cats, get_term_children($cat, 'category'));
     880                    $q['category__not_in'][] = $cat;
     881                    $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
    859882                }
    860883            }
    861             if ( ! empty($in_cats) ) {
    862                 $include_cats = "'" . implode("', '", $in_cats) . "'";
    863                 $include_cats = " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
    864             }
    865 
    866             if ( !empty($out_cats) ) {
    867                 $ids = get_objects_in_term($out_cats, 'category');
    868                 if ( is_array($ids) && count($ids > 0) ) {
    869                     $out_posts = "'" . implode("', '", $ids) . "'";
    870                     $exclude_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)";
    871                 }
    872             }
    873             $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
    874             $whichcat .= $include_cats . $exclude_cats;
     884        }
     885
     886        if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
    875887            $groupby = "{$wpdb->posts}.ID";
     888        }
     889
     890        if ( !empty($q['category__in']) ) {
     891            $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
     892            $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
     893            $include_cats = "'" . implode("', '", $q['category__in']) . "'";
     894            $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
     895        }
     896
     897        if ( !empty($q['category__not_in']) ) {
     898            $ids = get_objects_in_term($q['category__not_in'], 'category');
     899            if ( is_array($ids) && count($ids > 0) ) {
     900                $out_posts = "'" . implode("', '", $ids) . "'";
     901                $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
     902            }
     903        }
     904
     905        if ( !empty($q['category__and']) ) {
     906            $count = 0;
     907            foreach ( $q['category__and'] as $category_and ) {
     908                $join .= " LEFT JOIN $wpdb->term_relationships AS tr$count ON ($wpdb->posts.ID = tr$count.object_id) LEFT JOIN $wpdb->term_taxonomy AS tt$count ON (tr$count.term_taxonomy_id = tt$count.term_taxonomy_id) ";
     909                $whichcat .= " AND tt$count.term_id = '$category_and' ";
     910                $count++;
     911            }
    876912        }
    877913
Note: See TracChangeset for help on using the changeset viewer.