I’m wondering if the following setup can be more efficient without creating an taxonomy term page for every custom taxonomy term.

I’m working with courses (CPT) which has it’s own taxonomy (Course type).

Right now I have an archive template that lists all CPT by their taxonomy. I also have multiple taxonomy term templates for every taxonomy term like:

  • taxonomy-course-type-english.php
  • taxonomy-course-type-french.php
  • taxonomy-course-type-spanish.php

These template files uses a wp_query loop to show every post in the given taxonomy term. For example:

<?php $args = array(
     'posts_per_page' => -1,
     'post_type' => 'courses',
     'orderby' => 'date',
     'tax_query' => array(
                      array(
                 'taxonomy' => 'course-type',
                 'field'    => 'slug',
                'terms'    => 'french',
                ),
            ),
         );

The taxonomy-course-type.php template file list all posts from the taxonomy when using the default loop.

Isn’t there a way to use the taxonomy-course-type.php template file instead of the taxonomy terms pages where you have to create a new template file for every newly added taxonomy term?

1 Answer
1

Added this action hook to my functions.php file:

add_action( 'pre_get_posts', function ( $q )
{
if (    !is_admin() // Only target the front end queries
     && $q->is_main_query() // Targets the main query only
     && $q->is_tax('course-type') // Only target category pages
) {
    $q->set( 'posts_per_page', -1 );
      }
});

Then using the default loop on the taxonomy-course-type.php template file. Works really well without the need of any other even more specific taxonomy template files.

Leave a Reply

Your email address will not be published. Required fields are marked *