Title: Tom's Replies | WordPress.org

---

# Tom

  [  ](https://wordpress.org/support/users/tomdes/)

 *   [Profile](https://wordpress.org/support/users/tomdes/)
 *   [Topics Started](https://wordpress.org/support/users/tomdes/topics/)
 *   [Replies Created](https://wordpress.org/support/users/tomdes/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/tomdes/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/tomdes/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/tomdes/engagements/)
 *   [Favorites](https://wordpress.org/support/users/tomdes/favorites/)

 Search replies:

## Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)

 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Custom post type with custom category; show every category with all posts in it.](https://wordpress.org/support/topic/custom-post-type-with-custom-category-show-every-category-with-all-posts-in-it/)
 *  Thread Starter [Tom](https://wordpress.org/support/users/tomdes/)
 * (@tomdes)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/custom-post-type-with-custom-category-show-every-category-with-all-posts-in-it/#post-3451661)
 * Alright, it took me forever but I found a working solution. If you can use it,
   I really do hope that you find this post.
 * Here we go:
 *     ```
       $catargs=array(
       				'taxonomy' => 'machine_cat', //name of the custom taxonomy
       				'orderby' => 'name',
       				'parent' => 0 //get only the top level categories
       			);
       			$categories=get_categories($catargs);
       			foreach($categories as $category) {
       				echo '<h2>'.$category->name. ': ' . $category->term_id . '('. $category->slug .')</h2>'; //this outputs the category name followed by the term_id and the slug
   
       				$args = array(
       					'post_type' => 'machine_type', //take alle the posts from the custom post type 'machine_type'
       					'posts_per_page' => -1, //don't use pagination
       					'tax_query' => array(
       						array(
       							'taxonomy' => 'machine_cat', //get from the custom taxonomy 'machine_cat'
       							'field' => 'slug', //by slug
       							'terms' => $category->slug //the slug from within the foreach
       						)
       					)
       				);
   
       				$the_query = new WP_Query( $args ); //build a new WP_Query
   
       				// The Loop
       				while ( $the_query->have_posts() ) :
       					$the_query->the_post();
       					echo  get_the_title(); //for all **slug** from machine_cat, show the title
       				endwhile;
   
       				wp_reset_postdata();
   
       			} // end foreach
       ```
   
 * Sources:
    - [Stackoverflow](http://stackoverflow.com/questions/7688591/query-posts-by-custom-taxonomy-id)
    - [Codex: wp_list_categories](http://codex.wordpress.org/Function_Reference/get_categories)
    - [Codex: WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * Also important:
 * > At the moment it seems that you can’t filter the query further (as you can 
   > for posts) by adding an equivalent to:
   > ‘category__in’ => array(2,6)
 *  (found at [richardsweeney.com](http://richardsweeney.com/wordpress-3-0-custom-queries-post-types-and-taxonomies/))
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Archive of (only) custom post type, sorted by category](https://wordpress.org/support/topic/archive-of-only-custom-post-type-sorted-by-category/)
 *  Thread Starter [Tom](https://wordpress.org/support/users/tomdes/)
 * (@tomdes)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/archive-of-only-custom-post-type-sorted-by-category/#post-3132177)
 * [@deepbevel](https://wordpress.org/support/users/deepbevel/) thank you for the
   link, that solutions works (after I’ve changed “‘post_type’ => ‘post’,” to “‘
   post_type’ => ‘machine_type’,” of course).
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Archive of (only) custom post type, sorted by category](https://wordpress.org/support/topic/archive-of-only-custom-post-type-sorted-by-category/)
 *  Thread Starter [Tom](https://wordpress.org/support/users/tomdes/)
 * (@tomdes)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/archive-of-only-custom-post-type-sorted-by-category/#post-3132151)
 * @Mayeenul Islam Thank you for your response. I didn’t mention it but I did tried
   it. It works, but doesn’t sort by category (how could it, that isn’t in the code
   if I’m correct). The problem is that I don’t know how the number of categories
   there will be. So it’s not possible hard coding it.
 * [@deepbevel](https://wordpress.org/support/users/deepbevel/) Thank you for your
   response. I’ve already seen that answer, but the problem still stands (my apologies
   for not explaining it well in the first place): I don’t know the number of categories.
 * I’ve uploaded a screen shot of (a part of) my mock up on dropbox, you can find
   it here: [http://dl.dropbox.com/u/66836392/Schermafbeelding%20(7).png](http://dl.dropbox.com/u/66836392/Schermafbeelding%20(7).png).
   
   Here you see two categories (schocktestkasten and stresskasten) with posts (every
   image is another post). But I don’t know how many categories there will be.

Viewing 3 replies - 1 through 3 (of 3 total)