Question

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

Était-ce utile?

La solution

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

Autres conseils

If we want to display taxonomy name along with post title in backend MYSQL query you can use this query :-

SELECT   p.ID,taxonomy,post_title,post_status,name
FROM    wp_posts p, wp_term_relationships rel, wp_terms t, wp_term_taxonomy te
where  
   p.ID = rel.object_id
  AND t.term_id=te.term_id
  AND t.term_id=rel.term_taxonomy_id
  AND taxonomy in ('taxonomy')
  AND Post_type='post'
  and name='name of taxonomy'
order by post_title
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top