0

I am trying to make a list of all my posts in a custom post type by the taxonomy term e.g.

Term name 1 - post 1 - post 2 - post 3

Term name 2 - post 1 - post 2 - post 3

Term name 3 - post 1 - post 2 - post 3

I have found the function which lists the terms (get_terms) but can't work out a way to list the terms but also list the posts.

Any help is appreciated

  • Not a lot to be honest, I am more looking for a pointer in the right direction on how to achieve it – Gareth Gillman May 8 '14 at 10:07
  • 1
    You can get all the terms related to that post type and loop through them. Inside each loop, you can fetch the posts for that specific term. – Chittaranjan May 8 '14 at 10:46
  • I like your honesty @GarethGillman – Pieter Goosen May 8 '14 at 10:52
  • 1
    Sorted, I was over thinking it and it was as simple as listing the terms (via get_terms) and in the foreach doing a separate query for the posts for that taxonomy. – Gareth Gillman May 8 '14 at 10:59
  • 1
    To keep your question constructive and earn some reputation, please add your solution (code and a short description of the code) as an answer, and remember to come back in two days and accept your own answer. – Pieter Goosen May 8 '14 at 17:35
1

Sorted via get_terms and WP_Query

<?php
  $terms = get_terms("county");
  if ( !empty( $terms ) && !is_wp_error( $terms ) ){
   foreach ( $terms as $term ) {
    $my_query = new WP_Query('post_type=venues&posts_per_page=-1&county='.$term->name);
    while ($my_query->have_posts()) : $my_query->the_post();
     echo '<h2>'.$term->name.'</h2>';
     echo '<ul>';
      echo '<li>'.get_the_title().'</li>';
      echo '<li>'.get_the_content().'</li>';
     echo '</ul>';
    endwhile; wp_reset_query();
   }
  }
  ?>

I first get a list of the terms (using get_terms) and then querying the posts via WP_Query using the taxonomy option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.