I want outside assistance to rewrite this Wordpress query.

Sooner or later wordpress up-to-date the database to get rid of the post2cat table, and I wish to update this function to ensure that I'm able to apply it to my current wordpress website (v3.3.1 of wordpress).

The function lists all groups and also the posts connected with individuals groups.

In my opinion the issue in the function is isoltaed for this query, however i have incorporated the entire function below just just in case. Are you able to assist me to re-write this to operate around the new wordpress version?

$last_posts = (array)$wpdb->get_results("
    SELECT post_date, ID, post_title, cat_name, cat_ID
    FROM {$tp}posts, {$tp}post2cat, {$tp}categories 
    WHERE {$tp}posts.ID = {$tp}post2cat.post_id 
    AND {$tp}categories.cat_ID = {$tp}post2cat.category_id
    AND post_status = 'publish' 
    AND post_type != 'page' 
    AND post_date < NOW() 
    {$hide_check} 
    {$sort_code}
");

This is actually the full function. I am unsure if you will find other incompatibilities which i haven't spotted. And if anybody can point me towards a great resource that describes the datamodel of wordpress (within an readable way) please tell me :)

cheers

function posts_by_category() { 

global $wpdb, $post;

$tp = $wpdb->prefix;   

$pc_header = get_option('pc_header');

$sort_code = 'ORDER BY cat_name ASC, post_date DESC';
$the_output = NULL;


$last_posts = (array)$wpdb->get_results("
    SELECT post_date, ID, post_title, cat_name, cat_ID
    FROM {$tp}posts, {$tp}post2cat, {$tp}categories 
    WHERE {$tp}posts.ID = {$tp}post2cat.post_id 
    AND {$tp}categories.cat_ID = {$tp}post2cat.category_id
    AND post_status = 'publish' 
    AND post_type != 'page' 
    AND post_date < NOW() 
    {$hide_check} 
    {$sort_code}
");

if (empty($last_posts)) {
            echo 'no posts found';
    return NULL;
}

$the_output .= stripslashes($ddle_header); 

$used_cats = array();;
$i = 0;
foreach ($last_posts as $posts) {
    if (in_array($posts->cat_name, $used_cats)) {
        unset($last_posts[$i]);
    } else {
        $used_cats[] = $posts->cat_name;
    }
    $i++;
}
$last_posts = array_values($last_posts);

$the_output .= '<ul>';
foreach ($last_posts as $posts) {

  $the_output .= '<li><strong><a href="' . get_category_link($posts->cat_ID) . '">' . apply_filters('list_cats', $posts->cat_name, $posts) . '</a></strong></li>';

$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish' AND post_category=5" , $r );

$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select post_id FROM $wpdb->post2cat WHERE category_id =$posts->cat_ID) ORDER BY post_date DESC");

    foreach ( $arcresults as $arcresult ) {
    $the_output .= '<li><a href="' . get_permalink($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a></li>';
    }

$the_output .= '';
}
$the_output .= '</ul>';
return $the_output;
}


function pc_generate($content) {
$content = str_replace("<!-- postsbycategory -->", posts_by_category(), $content);
return $content;
}

 add_filter('the_content', 'pc_generate');
 add_action('admin_menu', 'pc_add_option_pages');