WordPress.org

Make WordPress Core

Changeset 3767


Ignore:
Timestamp:
05/08/2006 02:16:24 AM (12 years ago)
Author:
ryan
Message:

Category query cleanups from skeltoac. fixes #2709

File:
1 edited

Legend:

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

    r3697 r3767  
    500500
    501501        // First let's clear some variables
     502        $distinct = '';
    502503        $whichcat = '';
    503504        $whichauthor = '';
     
    693694            $q['cat'] = ''.urldecode($q['cat']).'';
    694695            $q['cat'] = addslashes_gpc($q['cat']);
    695             if (stristr($q['cat'],'-')) {
    696                 // Note: if we have a negative, we ignore all the positives. It must
    697                 // always mean 'everything /except/ this one'. We should be able to do
    698                 // multiple negatives but we don't :-(
    699                 $eq = '!=';
    700                 $andor = 'AND';
    701                 $q['cat'] = explode('-',$q['cat']);
    702                 $q['cat'] = intval($q['cat'][1]);
    703             } else {
    704                 $eq = '=';
    705                 $andor = 'OR';
    706             }
    707696            $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
    708697            $cat_array = preg_split('/[,\s]+/', $q['cat']);
    709             $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
    710             $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
    711             for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
    712                 $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
    713                 $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
    714             }
    715             $whichcat .= ')';
    716             if ($eq == '!=') {
    717                 $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category.
    718             }
     698            $in_cats = $out_cats = '';
     699            foreach ( $cat_array as $cat ) {
     700                $in = strstr($cat, '-') ? false : true;
     701                $cat = trim($cat, '-');
     702                if ( $in )
     703                    $in_cats .= "$cat, " . get_category_children($cat, '', ', ');
     704                else
     705                    $out_cats .= "$cat, " . get_category_children($cat, '', ', ');             
     706            }
     707            $in_cats = substr($in_cats, 0, -2);
     708            $out_cats = substr($out_cats, 0, -2);
     709            if ( strlen($in_cats) > 0 )
     710                $in_cats = " AND category_id IN ($in_cats)";
     711            if ( strlen($out_cats) > 0 )
     712                $out_cats = " AND category_id NOT IN ($out_cats)";
     713            $whichcat = $in_cats . $out_cats;
     714            $distinct = 'DISTINCT';
    719715        }
    720716
     
    747743            $tables = ", $wpdb->post2cat, $wpdb->categories";
    748744            $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    749             $whichcat = " AND (category_id = '" . $q['cat'] . "'";
    750             $whichcat .= get_category_children($q['cat'], " OR category_id = ");
     745            $whichcat = " AND category_id IN ({$q['cat']}, ";
     746            $whichcat .= get_category_children($q['cat'], '', ', ');
     747            $whichcat = substr($whichcat, 0, -2);
    751748            $whichcat .= ")";
     749            $distinct = 'DISTINCT';
    752750        }
    753751
     
    878876            $groupby = 'GROUP BY ' . $groupby;
    879877        $join = apply_filters('posts_join_paged', $join);
    880         $orderby = apply_filters('posts_orderby', $q['orderby']);
    881         $request = " SELECT * FROM $wpdb->posts $join WHERE 1=1 $where $groupby ORDER BY $orderby $limits";
     878        $orderby = apply_filters('posts_orderby', $q['orderby']);
     879        $distinct = apply_filters('posts_distinct', $distinct);
     880        $fields = apply_filters('posts_fields', "$wpdb->posts.*");
     881        $request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby ORDER BY $orderby $limits";
    882882        $this->request = apply_filters('posts_request', $request);
    883883
Note: See TracChangeset for help on using the changeset viewer.