| 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 | |