Associating Post Category with Post using SQL
-
I am modifying Alex King’s plugin Popularity Contest in order to get it to print out a post category next to the post title. The current code is:
$posts = $wpdb->get_results(" SELECT ID, post_title, comment_count FROM $wpdb->posts LEFT JOIN $wpdb->ak_popularity pop ON $wpdb->posts.ID = pop.post_id $join WHERE post_status = 'publish' AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( '<li class="' . $oddli . '"><h4><a href="'.get_permalink($post->ID).'">' .$post->post_title.'</a></h4><p class="meta">Topics:' . [CATEGORY GOES HERE] . ' / <a href="' .get_permalink($post->ID) . '">'. $post->comment_count . ' comments</a></p></li>' ); /* Changes every other <li> to a different class */ if ('alt' == $oddli) $oddli = ''; else $oddli = 'alt'; } }I need to somehow LEFT JOIN the terms, term_taxonomy, etc. tables and then put the category into the code. Problem: I have no idea how to do this.
The topic ‘Associating Post Category with Post using SQL’ is closed to new replies.