Title: Associating Post Category with Post using SQL
Last modified: August 19, 2016

---

# Associating Post Category with Post using SQL

 *  [benblei](https://wordpress.org/support/users/benblei/)
 * (@benblei)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/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.

 * 0 replies
 * 1 participant
 * Last reply from: [benblei](https://wordpress.org/support/users/benblei/)
 * Last activity: [18 years, 2 months ago](https://wordpress.org/support/topic/associating-post-category-with-post-using-sql/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
