Skip to content

Instantly share code, notes, and snippets.

@mrkaluzny
Last active August 14, 2017 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkaluzny/5a9f92a55e896a921e0dc729e4c0452a to your computer and use it in GitHub Desktop.
Save mrkaluzny/5a9f92a55e896a921e0dc729e4c0452a to your computer and use it in GitHub Desktop.
WordPress Custom Post Type Loop with Taxonomies categorization on page.
<?php
// Setting up the query
$post_type_name = 'faculty';
$taxonomy_name = 'faculty-type';
$terms_arr = get_terms($taxonomy_name);
$terms_to_loop = returnTermsSlug($terms_arr);
function returnTermsSlug($termsArr) {
$a = [];
foreach ($termsArr as $term) {
array_push($a, $term->slug);
}
return $a;
}
function returnQueryForPostType($post_type, $taxonomy) {
return new WP_Query(array(
'post_type' => $post_type,
'faculty-type' => $taxonomy,
'posts_per_page' => -1,
));
}
foreach ($terms_to_loop as $term) :
?>
<div class="teachers-list__category">
<?php if($term != 'general' ) : ?>
<div class="teachers-list__category__name">
<?php echo str_replace('-', ' ', $term); ?>
</div>
<?php endif; ?>
<?php
$current_query = returnQueryForPostType($post_type_name, $term);
if ($current_query->have_posts()) : while($current_query->have_posts()) : $current_query->the_post();
?>
<div class="teacher-item">
<div class="teacher-item__name"><?php the_title(); ?></div>
</div>
<?php endwhile; endif; ?>
</div>
<?php endforeach; wp_reset_query(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment