Ticket #4750: cat__in.diff
File cat__in.diff, 5.5 KB (added by , 18 years ago) |
---|
-
wp-includes/query.php
413 413 $array[$key] = ''; 414 414 } 415 415 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 } 416 422 return $array; 417 423 } 418 424 … … 548 554 $this->is_category = true; 549 555 } 550 556 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 551 577 if ( '' != $qv['tag'] ) 552 578 $this->is_tag = true; 553 579 … … 842 868 } else { 843 869 $q['cat'] = ''.urldecode($q['cat']).''; 844 870 $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) ";846 871 $cat_array = preg_split('/[,\s]+/', $q['cat']); 847 $in_cats = $out_cats = array();848 $include_cats = $exclude_cats = '';849 872 foreach ( $cat_array as $cat ) { 850 873 $cat = intval($cat); 851 874 $in = ($cat > 0); 852 875 $cat = abs($cat); 853 876 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')); 856 879 } 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')); 859 882 } 860 883 } 861 if ( ! empty($in_cats) ) { 862 $include_cats = "'" . implode("', '", $in_cats) . "'"; 863 $include_cats = " AND $wpdb->term_taxonomy.term_id IN ($include_cats) "; 884 } 885 886 if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) { 887 $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)"; 864 902 } 903 } 865 904 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 }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++; 872 911 } 873 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";874 $whichcat .= $include_cats . $exclude_cats;875 $groupby = "{$wpdb->posts}.ID";876 912 } 877 913 878 914 if ( '' != $q['tag'] ) { -
wp-includes/classes.php
3 3 class WP { 4 4 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'); 5 5 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'); 7 7 var $extra_query_vars = array(); 8 8 9 9 var $query_vars; … … 17 17 $this->public_query_vars[] = $qv; 18 18 } 19 19 20 function set_query_var($key, $value) { 21 $this->query_vars[$key] = $value; 22 } 23 20 24 function parse_request($extra_query_vars = '') { 21 25 global $wp_rewrite; 22 26