Grouping posts by Custom Taxonomies and ording by date.
-
I have already figured out how to create a loop that pulls posts from a Custom Posts Type that I created and grouping them by terms of a Custom Taxonomies.
Sorting of terms Alphabetically works perfect, but when I try to sort by date things go whacky.
The first group it displays is from December 2014, then next from Early 2015, Early 2014, Fall 2013, Summer 2014. Basically, the groups orders are all over the place by date.
I am wondering if this has to do with editing the published on date. With this particular custom posts type, I am creating posts that link to photographers pages to view their pictures they took of a particular band and I’m setting the Published Date to the date of the particular concert. So I’m wondering if my issue is coming from the posts in the database not being in sequential order with their posts date or if I am just doing something wrong.
Here is my code for this loop:
<?php /* Get Concert Photography by Tour */ $categories = get_terms('tour', 'orderby=post_date&order=ASC&hide_empty=1'); foreach( $categories as $category ): ?> <div class="related-article"> <h2 class="box-title"><?php echo $category->name; // Prints the cat/taxonomy group title ?></h2> <ul class="row"> <?php $posts = get_posts(array( 'post_type' => 'concert-photography', 'taxonomy' => $category->taxonomy, 'term' => $category->slug, 'nopaging' => true, )); foreach($posts as $post): setup_postdata($post); //enables the_title(), the_content(), etc. without specifying a post ID ?> <?php $post_thumbnail = get_the_post_thumbnail( get_the_ID(), 'thumbnail' ); $class_format = ''; if ( ! $post_thumbnail ) $_class_format = 'fa-format-' . get_post_format( get_the_ID() ); printf( '<li class="col-md-6"> <a href="%s" class="post-thumbnail %s">%s</a> <div class="related-post-content"> <a class="related-post-title" href="%s"> <span class="date">%s</span></br> %s</a> </div> </li>', esc_url( get_permalink() ), $class_format, $post_thumbnail, esc_url( get_permalink() ), get_the_title(), get_the_date() ); ?> <?php endforeach; ?> </ul> </div> <?php endforeach; ?> <?php wp_reset_query(); ?>
Anyone have any suggestions on how to get the sort by date of the terms so that It is ordered Spring 2015, December 2014, Fall 2014, Summer 2014 And Spring 2014 and not be all over the place as it is right now?
Thanks
- The topic ‘Grouping posts by Custom Taxonomies and ording by date.’ is closed to new replies.