title: Joining tags table to posts table in Codeigniter
body: <p>I need some help retrieving posts that belong to a specific group of tags. I am working with Codeigniter and MySQL.</p>
  
  <p>My tables look like this:</p>
  
  <p><strong>posts</strong> - postid, title, date, etc</p>
  
  <p><strong>tags</strong> - tagid, tag</p>
  
  <p><strong>post_tags</strong> - postid, tagid</p>
  
  <p>This is the function I am using in my model:</p>
  
  <pre><code>function get_posts($tags) {
  
      $this-&gt;db-&gt;select('*');
      $this-&gt;db-&gt;from('posts');
      $this-&gt;db-&gt;order_by('date', 'DESC');
      $this-&gt;db-&gt;join('post_tags', 'posts.postid = post_tags.postid');
      $this-&gt;db-&gt;join('tags', 'post_tags.tagid = tags.tagid');
      $this-&gt;db-&gt;where_in('tags.tag', $tags);
      $q = $this-&gt;db-&gt;get();
      return $q-&gt;result();
  }
  </code></pre>
  
  <p>So far, the query is working the way I want and returning all the proper posts. </p>
  
  <p>However, while looping through the result and setting up the specific post information, only the <em>first tag</em> is being listed per post.</p>
  
  <p>Is it possible to restucture the query so that each post includes <em>all</em> the tags that match the original $tags array?</p>
  
  <p>Thanks!</p>