Make WordPress Core

Ticket #1625: 1625.diff

File 1625.diff, 3.5 KB (added by drac, 19 years ago)
  • wp-includes/functions.php

     
    11881188}
    11891189
    11901190function get_posts($args) {
     1191        // support for multiple categories and negations added: 9-nov-05 drac
    11911192        global $wpdb;
    11921193        parse_str($args, $r);
    11931194        if ( !isset($r['numberposts']) )
     
    12001201                $r['orderby'] = 'post_date';
    12011202        if ( !isset($r['order']) )
    12021203                $r['order'] = 'DESC';
     1204       
     1205        $negation_char = '^';
     1206        $multiple_categories_present = false;
     1207        if( is_array($r['category']) ) {
     1208                $multiple_categories_present = true;
     1209        }
    12031210
    12041211        $now = current_time('mysql');
    12051212
    1206         $posts = $wpdb->get_results(
    1207                 "SELECT DISTINCT * FROM $wpdb->posts " .
     1213        $sql = "SELECT DISTINCT * FROM $wpdb->posts " .
    12081214                ( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) .
    1209                 " WHERE post_date <= '$now' AND (post_status = 'publish') ".
    1210                 ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) .
    1211                 " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] );
     1215                " WHERE post_date <= '$now' AND (post_status = 'publish') ";
    12121216
     1217        if ( false == $multiple_categories_present ) {  // check for a single category first
     1218
     1219                if ( strpos($r['category'], $negation_char) !== false ) {               // category is a negation. note: strpos requires a bool comparison operator
     1220                        $r['category'] = strtr($r['category'], $negation_char, " ");
     1221                        $sql .= ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id != " . $r['category']. " " );
     1222                }
     1223                else {                                                                                                 
     1224                        $sql .= ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " );
     1225                }
     1226        }
     1227
     1228        else {                  // we have multiple categories to select from. yay
     1229                $included_categories = array();
     1230                $negated_categories = array();                                         
     1231                $have_categories = false;                                                       // are any categories actually sent through to us ? this boolean will prevent unnecessary SQL being generated
     1232
     1233                foreach ( $r['category'] as $category_param_element ) { // multiple category params are possible, so iterate through all of them
     1234                        $cat_tokens = split(' ', $category_param_element);     
     1235                        foreach ( $cat_tokens as $token ) {                             
     1236                                if ( strpos($token, $negation_char) !== false ) { // you are a category to be negated from display.     n.b: strpos wants a bool comparison operator
     1237                                        $token = strtr($token, $negation_char, " ");
     1238                                        array_push($negated_categories, $token);                                       
     1239                                }
     1240                                else {
     1241                                        array_push($included_categories, $token);                                       
     1242                                }
     1243                        }
     1244                        if( ( !$have_categories ) && count ( $cat_tokens > 0 ) ) {
     1245                                $have_categories = true;
     1246                        }
     1247                }
     1248               
     1249                if ( $have_categories ) { // there is at least one category to be included or excluded
     1250                        $sql .= "AND $wpdb->posts.ID = $wpdb->post2cat.post_id ";
     1251                }
     1252
     1253                if ( 0 != count ($included_categories)  )  {
     1254                        $sql .=  "AND $wpdb->post2cat.category_id in ( " . implode(",", $included_categories) . " ) " ;
     1255                }
     1256                if ( 0 != count($negated_categories) )  {
     1257                        $sql .=  "AND $wpdb->post2cat.category_id not in ( " . implode(",", $negated_categories) . " ) ";
     1258                }
     1259        }
     1260
     1261        $sql .= " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] ;
     1262       
     1263        $posts = $wpdb->get_results( $sql );
     1264
    12131265        update_post_caches($posts);
    12141266
    12151267        return $posts;