It’s documented, it’s used, but it wasn’t obvious to me. After hunting around, I finally found what I needed in the first place I looked: the WordPress Codex.
This is the wp_query argument needed to display posts assigned a particular term:
1 2 3 4 5 6 |
'tax_query' => array(array( 'taxonomy' => 'site-categories', 'field' => 'slug', 'terms' => 'telephony' ) ) |
Using the term ID as a cat ID wasn’t working. I came across the above here, and used it in two simple loops to display the custom posts under category headers.
An error has occurred. Please try again later. |
Want to display posts in a nested hierarchy? This goal led me to what is probably a cleaner solution. The following code is adapted from Hierarchical Category List with Post Titles, and blended with the code above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
function my_custom_post_type_terms() { global $wp_query,$paged,$post; $temp = $wp_query; $wp_query= null; ob_start(); $args = array( 'orderby' => 'name', 'order' => 'ASC', 'taxonomy' => 'custom-taxonomony-slug' ); $categories = get_categories($args); foreach($categories as $category) : if (!$category->parent) { ?> <h2 class="title"><?php echo $category->name; ?></h2> <?php process_cat_tree($category->term_id); } endforeach; $wp_query = null; $wp_query = $temp; $content = ob_get_contents(); ob_end_clean(); return $content; } function process_cat_tree($cat) { global $wp_query,$paged,$post; $args = array('tax_query' => array( array( 'taxonomy' => 'custom-taxonomony-slug', 'field' => 'id', 'terms' => $cat, 'include_children' => false ) ), 'numberposts' => -1, 'orderby' => 'name', 'order' => 'ASC'); $wp_query = new WP_Query($args); ?> <ul class="sites-list"> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <li> <p><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></p> <?php the_content(); ?> </li> <?php endwhile; ?> </ul> <?php $next = get_categories(array( 'taxonomy' => 'custom-taxonomony-slug', 'parent' => $cat)); if($next) : foreach( $next as $category ) : ?> <h3 class="sub-cat-title"><?php echo $category->name; ?></h3> <?php process_cat_tree( $category->term_id ); endforeach; endif; } |
It’s in action here under Sites…
Hi Daniel,
I’ve been searching for a solution like this, however, I am getting PHP errors at line 23 due to the extra ‘}’, and, in my functions.php due to something else that I haven’t been able to identify. I’d love to implement this snippet, could you check that everything is as it should be?
Many thanks,
Owen
Oops, the functions.php error has nothing to do with this, but the unexplained ‘}’ is definitely confusing things.
Hi everyone,
I’ve altered the code above, adding the first line now shown. I think the issue reported is simply that the first function isn’t declared as a function.
This isn’t a complete solution. You’ll need to call the first function, like in a shortcode or other hook.