I've seen this question posted in different forms before, but most of those solutions are centered around the admin interface, and I haven't found any answers that apply to the front end.

I've got a custom post type, and an associated custom taxonomy. I'm using the archive page (archive-{custom_type}.php) to display the items, and using wp_list_categories to show a list of the custom taxonomy terms. I can manually alter the posts displayed by adding a tax_query parameter to the WP_Query call, but the problem I'm running into is I cannot figure out how to alter the taxonomy links so they point to this archive page so I can filter dynamically. I'd rather not duplicate this template's markup and code in a taxonomy-{custom_type}.php file.

Do I need to just output the taxonomy links manually? How should the URL be structured so I can get the query param? I have query_var => true set and a rewrite rule on the custom taxonomy definition, but haven't been able to get get_query_var() to return anything.

The end result should be a template that can list all items in the custom post type, or filter those items by their associated custom taxonomy.

  • 2
    I frequently need this on sites and I always find it neater to duplicate the template. To prevent duplicate code though, you can use get_template_part() to pull a common piece of PHP into both templates, or even to pull one template into the other. Commented May 19, 2016 at 14:52

2 Answers 2


Let say you have "book" post type and "genre" taxonomy. And you want to get books with genre of "scifi".

You can pass the parameter in the url using:


Then you can get those parameter using get_query_var('taxonomy') and get_query_var('term') and add them to the WP_Query arguments.

$taxonomy = get_query_var('taxonomy');
$term = get_query_var('term');

$args = array(
    'post_type' => 'book',
    'tax_query' => array(
            'field'    => 'slug',
            'taxonomy' => $taxonomy,
            'terms'    => $term,
$query = new WP_Query($args);

First I have taxonomy "service".

Here I make a different php file, for the taxonomy "service" loop and the default WordPress loop (post).

which will be called in archive.php

it will look the same

First I will create a file for the "service" taxonomy named my-loop-service.php

Fill in the code:

<div class="my-service">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <div <?php post_class('my-service-loop'); ?>>
           <h1 class="my-service-title"><?php the_title(); ?></h2>
<?php endwhile; endif; ?>

Secondly I make a file for the default post loop with the name my-loop-post.php

Fill in the code:

<div class="my-post">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <div <?php post_class('my-post-loop'); ?>>
           <h1 class="my-post-title"><?php the_title(); ?></h2>
<?php endwhile; endif; ?>

And the following is the contents of the archive.php file


if (is_post_type_archive('service') || is_tax('services-category') || is_tax('services-tags')){
    get_template_part('my-loop-service'); // Get Loop for Taxonomy service
} else {
   get_template_part('my-loop-post'); // Default Loop for Post


    // Navigation
    echo '<div id="my-navigation">';
    the_posts_pagination( array(
        'mid_size' => 2,
        'prev_text' => __( 'Prev', 'text-domain' ),
        'next_text' => __( 'Next', 'text-domain' ),
    echo '</div>';


That way there is no need to create a special php archive file for your taxonomy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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