I’m trying to setup something in my index file where I can order the posts by a custom tax. The custom tax I would like to sort by is year_published.
I’ve already accomplished this but I can’t get it to work with category filters now.
Here’s my example of sorting the posts by year_published.
<?php $categories = get_terms( 'year_published', 'orderby=title&order=DESC&hide_empty=1' ); foreach( $categories as $category ): ?> <?php $posts = get_posts(array( 'taxonomy' => $category->taxonomy, 'term' => $category->slug, 'nopaging' => FALSE, )); foreach($posts as $post): setup_postdata($post); ?> <?php get_template_part('templates/content', get_post_format()); ?> <?php endforeach; // categories ?> <?php endforeach; // posts?>
Here’s the bit of code I’m referring to in the index.php file.
<?php while (have_posts()) : the_post(); ?> <?php get_template_part('templates/content', get_post_format()); ?> <?php endwhile; ?>
This is the default setup, and it works great with filtering categories. e.g. example.com/category/category-term
I need something that will allow my posts to display by published but also allow the user to use category filters. Is that possible?
I’m thinking maybe it’s two separate index.php files. The first would be the regular index.php with sorting by tax and the other would be specific to a category condition “index-category.php”. I’m not sure if that’s the right path but any help is appreciated.