$sql_exclude_cats = ''; if (!empty($excluded_categories)) { ### ronr hack BEGIN ### # find ids of all posts in excluded cats $query = "SELECT post_id FROM $wpdb->post2cat WHERE category_id IN (" . str_replace(',', ',', $excluded_categories) . ')'; $excluded_post_rows = $wpdb->get_results($query); # build IN clause from those ids if (count($excluded_post_rows) > 0) { $sql_exclude_cats = ' AND ID NOT IN (' . intval($excluded_post_rows[0]->post_id); for ($i = 1; $i < (count($excluded_post_rows)); $i++) { $sql_exclude_cats .= ',' . intval($excluded_post_rows[$i]->post_ id); } $sql_exclude_cats .= " )"; } ### ronr hack END ### }