Created
August 7, 2012 19:02
-
-
Save ericrasch/3288425 to your computer and use it in GitHub Desktop.
List WordPress Custom Taxonomy with Posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$taxonomyName = "cpt_rankings_cat"; // Set your custom taxonomy here. | |
// Alternate method for retrieving a term ID. Source: http://codex.wordpress.org/Function_Reference/wp_get_post_terms#Examples | |
$term_list = wp_get_post_terms($post->ID, $taxonomyName, array("fields" => "ids")); | |
// Source: http://codex.wordpress.org/Function_Reference/get_term_children#A_Basic_Example | |
$term_parent_ID = $term_list[0]; // Pulls the parent term ID from the call above. | |
// Use the following get_terms in order to have them sort by name. Source: http://wordpress.stackexchange.com/a/17148 | |
$term_children = get_terms( $taxonomyName, array( | |
'child_of' => $term_parent_ID, | |
'orderby' => 'term_group', | |
'order' => 'ASC', | |
) ); | |
foreach ($term_children as $term_child) { | |
$term_args = array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => $taxonomyName, | |
'field' => 'slug', | |
'terms' => $term_child->slug, // Calling the single child term here. | |
), | |
), | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'date', | |
'order' => 'DESC', | |
); // END $term_args | |
$term_query = null; | |
$term_query = new WP_Query($term_args); | |
if( $term_query->have_posts() ) { | |
echo '<h3>' . $term_child->name . '</h3>'; // Placed the term name here so it won't display if it doesn't have any posts. | |
echo '<p>' . $term_child->description . '</p>'; // If your term has a description, it will show. | |
echo '<ul>'; | |
while ($term_query->have_posts()) : $term_query->the_post(); ?> | |
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | |
</li> | |
<?php | |
endwhile; // END while have_posts | |
echo '</ul>'; | |
} // END if have_posts | |
wp_reset_postdata(); | |
} // END foreach | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment