Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Tom

    (@tomdes)

    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:

    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)

    Thread Starter Tom

    (@tomdes)

    @deepbevel thank you for the link, that solutions works (after I’ve changed “‘post_type’ => ‘post’,” to “‘post_type’ => ‘machine_type’,” of course).

    Thread Starter Tom

    (@tomdes)

    @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 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.
    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)