Changeset 5873

Show
Ignore:
Timestamp:
08/15/07 22:08:51 (1 year ago)
Author:
ryan
Message:

Category union and intersection query vars. fixes #4750

Files:

Legend:

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

    r5708 r5873  
    44    var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots'); 
    55 
    6     var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type', 'post_status'); 
     6    var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and'); 
    77    var $extra_query_vars = array(); 
    88 
     
    1616    function add_query_var($qv) { 
    1717        $this->public_query_vars[] = $qv; 
     18    } 
     19 
     20    function set_query_var($key, $value) { 
     21        $this->query_vars[$key] = $value; 
    1822    } 
    1923 
  • 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