I have a custom post type: ‘course’.
Inside ‘course’ are 2 taxonomies:
- ‘course_type’ (Full Time Courses, Part Time Courses)
- ‘course_attribute’ (Business, Design, Hospitality)
How can I list all posts along with all ‘course_attribute’ as headers?
I want the list to appear as:
Full Time Courses
Business
- Business Course 01
- Business Course 02
Design
- Design Course 01
- Design Course 02
Hospitality
- Hospitality Course 01
- Hospitality Course 02
Part Time Courses
Business
- Business Course 01
- Business Course 02
Design
- Design Course 01
- Design Course 02
Hospitality
- Hospitality Course 01
- Hospitality Course 02
Current code is not really working as I expected it to be and it is not hiding course types that are empty.
<?php
$terms = get_terms( 'course_attributes', array(
'orderby' => 'count',
'hide_empty' => true
) );
?>
<div class="col-md-6">
<h2>Full Time Courses</h2>
<?php
foreach( $terms as $term ) {
$args = array(
'hide_empty' => true,
'post_type' => 'course',
'course_attributes' => $term->slug,
'tax_query' => array(
array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => 'full-time-courses',
'hide_empty' => true
)
),
);
$query = new WP_Query( $args );
echo'<p><strong>' . $term->name . '</strong></p>';
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
echo '</ul>';
wp_reset_postdata();
} ?>
</div>
<div class="col-md-6">
<h2>Part Time Courses</h2>
<?php
foreach( $terms as $term ) {
$args = array(
'post_type' => 'course',
'course_attributes' => $term->slug,
'tax_query' => array(
array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => 'part-time-courses',
)
),
);
$query = new WP_Query( $args );
echo'<p><strong>' . $term->name . '</strong></p>';
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
echo '</ul>';
wp_reset_postdata();
} ?>
</div>