Question

I have a wordpress custom post type of "awards", which has a custom taxonomy of "awardyear." I'm trying to write a query that will list them all out, but group them by year, and have the year in a heading. For example all of the awards from the year 2010 would be grouped together & have "2010" as the header. Is this even possible? I'm able to get them all listed out, however, it's currently listing them all out under each year. Here is my current query:

$taxonomy = 'awardyear';
$queried_term = get_query_var($taxonomy);
$terms = get_terms($taxonomy, 'slug='.$queried_term);

$args = array( 
'post_type' => 'awards',
'posts_per_page' => -1 ,
'awardyear' => $term->name,
'order_by' => 'awardyear',
'order' => 'ASC'    
 );

if ($terms) {
  foreach($terms as $term) {
    echo '<span class="award_year">' . $term->name . '</span> ';
    query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
    echo '<li style="list-style: none;">';
    echo '<a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '</a>';
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();

  }
}

Example output would be:

2010
Award Example Number 1
Award Example Number 2

2011
Award Example Number 1
Award Example Number 2
Award Example Number 3
Award Example Number 4

2012
Award Example Number 1
Award Example Number 2
Award Example Number 3
Was it helpful?

Solution

Your problem is that $args['awardyear'] gets set, once, before $term is defined and will therefore be undefined itself. Remove it from the definition of $args and put $args['awardyear'] = $term->name; at the top of your foreach loop. That way it will be set correctly on each past.

Please note that this sort of use of query_posts() is generally frowned upon; see the Codex entry for that function.

OTHER TIPS

You may want to consider doing it a different way as post meta would be a better choice on actually accomplishing what you want effectively.

Although regardless if you really want to use the taxonomy you will need to do it based on a query that takes the taxonomy columns and you can display it from there.

Reference: http://scribu.net/wordpress/sortable-taxonomy-columns.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top