title: WordPress - Getting Posts by Custom Post Type and Category, with each category automatically encased in a DIV
body: <p>Sorry for the long question title.  I was attempting to be accurate.</p>
  
  <p>I need to devise a WordPress query that will automatically get posts from a certain custom post type, detect how each post is categorized, and then output the posts, by category onto a page, with each category encased in its own DIV.</p>
  
  <p>For example, I have a custom post type called "Map Data".  Within this custom post type, I have a heirarchial taxonomy that I have named "Categories", and within that taxonomy, a number of categories, "Category #1", "Category #2", so on and so forth.  Each Category has a number of posts.</p>
  
  <p>So, the query should get a list of all of the categories within the custom post type, and then output something like this:</p>
  
  <pre><code>&lt;div id="category-1"&gt;
       &lt;div class="post"&gt;This is a post in Category 1&lt;/div&gt;
       &lt;div class="post"&gt;This is another post in Category 1&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="category-2"&gt;
       &lt;div class="post"&gt;This is a post in Category 1&lt;/div&gt;
       &lt;div class="post"&gt;This is another post in Category 1&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have the following code which works with the default WordPress Category system, however, I either need to re-write it, or update it so that it can work with custom post types and their taxonomies.</p>
  
  <pre><code>&lt;?php 
      $cat_args=array();
      $categories=get_categories($cat_args);
      foreach($categories as $category) {
          $args=array(
              'category__in' =&gt; array($category-&gt;term_id),
          );
      $posts=get_posts($args);
          if ($posts) {
              echo '&lt;div class="cat" id="' . $category-&gt;slug.'" name="' . $category-&gt;name.'"&gt;';
              foreach($posts as $post) {
            setup_postdata($post); 
  ?&gt;
  
  &lt;?php the_title();?&gt;
  &lt;?php the_content();?&gt;
  
  &lt;?php
          } // foreach($posts
          echo '&lt;/div&gt;';
        } // if ($posts
      } // foreach($categories
  ?&gt;
  </code></pre>
  
  <p>If anyone could provide updated code for me to try, or a working example, it would be very much appreciated.</p>