post type taxonomy loop wordpress

Solutions on MaxInterview for post type taxonomy loop wordpress by the best coders in the world

showing results for - "post type taxonomy loop wordpress"
Linus
23 Oct 2017
1<?php
2// Get all the categories
3$categories = get_terms( 'service-category' );
4
5// Loop through all the returned terms
6foreach ( $categories as $category ):
7
8    // set up a new query for each category, pulling in related posts.
9    $services = new WP_Query(
10        array(
11            'post_type' => 'services',
12            'showposts' => -1,
13            'tax_query' => array(
14                array(
15                    'taxonomy'  => 'service-category',
16                    'terms'     => array( $category->slug ),
17                    'field'     => 'slug'
18                )
19            )
20        )
21    );
22?>
23
24<h3><?php echo $category->name; ?></h3>
25<ul>
26<?php while ($services->have_posts()) : $services->the_post(); ?>
27    <li><?php the_title(); ?></li>
28<?php endwhile; ?>
29</ul>
30
31<?php
32    // Reset things, for good measure
33    $services = null;
34    wp_reset_postdata();
35
36// end the loop
37endforeach;
38?>