Custom posts type looping with custom taxonomies in WordPress

Loop any terms assigned to the custom post type along with custom taxonomy and display on the page

<?php
// Get all the categories
$categories = get_terms( 'tender-category' );

// Loop through all the returned terms
foreach ( $categories as $category ):

    // set up a new query for each category, pulling in related posts.
    $tenders = new WP_Query(
        array(
            'post_type' => 'tenders',
            'showposts' => -1,
            'tax_query' => array(
                array(
                    'taxonomy'  => 'tender-category',
                    'terms'     => array( $category->slug ),
                    'field'     => 'slug'
                )
            )
        )
    );
?>

<h3><?php echo $category->name; ?></h3>
<ul>
<?php while ($tenders->have_posts()) : $tenders->the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>

<?php
    // Reset things, for good measure
    $tenders = null;
    wp_reset_postdata();

// end the loop
endforeach;
?>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.