2

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?

  • Did you just delete your answer which was really helpfull? – NielsPilon Jul 11 '16 at 10:50
  • Got is sorted using a get_pre_post action hook. Thanks to Pieter Goosen for pointing me in the right direction. – NielsPilon Jul 11 '16 at 12:50
  • Post your comment as an answer and close this question please as it will irritatebly be rotated by the community bot as unanswered – Pieter Goosen Jul 11 '16 at 16:10
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.

  • Upvote from me - proper use of pre_get_posts is much better than throwing out and replacing the main query. If this solves your problem, then accept your own answer so the question is marked as resolved! – Andy Macaulay-Brook Jul 21 '16 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.