I have a custom post type for which I have created a custom taxonomy called 'tag'. taxonomy-tag.php file looks like this:

<?php get_header(); ?>

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

      <h1 class="page-title"><?php echo $term->name; ?> Archives</h1>

      <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>

          <div class="post type-post hentry">

              <a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_title(); ?>

        <?php endwhile; ?>
      <?php endif; ?>

<?php get_footer(); ?>

When this code is executed, it displays only things outside the loop. The loop find no result even if I have many posts of the specific custom post type bind to many terms of the custom taxonomy.

Have you faced such issue? I am stuck and your help is highly invaluable.

  • 1
    I'm pretty sure tag is a reserved keyword ($tag, $_GET['tag'] etc should not be used) in WordPress, as well as a taxonomy already defined by WordPress (for posts).
    – powerbuoy
    Jun 9 '13 at 23:12
  • Yes @powerbuoy, and that was the problem. Thanks for your answer, if only it comes a few time before I could resolve the problem earlier. Thanks anyway, you are right Jun 9 '13 at 23:42

You solved this yourself by running a new query, but a new query should not be necessary as @Milo states in the comments to your question. The main query should be doing automatically what your secondary query is doing.

I suspect this has something to do with the way you have registered that custom post type, since I am able to duplicate this problem only when I register a type with a 'public' => false argument. There may be other arguments that could do this, or argument combinations, but that is the only one I found in my less than exhaustive search. Use 'public' => true when you register the post type, if you are not already doing so.

  • Thank you @s_ha_dum but my post type is already registred with 'public' => true. Jun 9 '13 at 19:12
  • Dear S_ha_dum, I wrote the solution that I found to my problem, thank you for your time. Jun 9 '13 at 20:30


At the beginning I thought I resolved my problem after creating a new query with the arguments I need. The code I added is:


$args = array(
    'paged' => $paged,
    'post_type' => 'post_type_name',
    'tax_query' => array(
                      array('taxonomy' => 'tag','terms' =>  $term->term_id)));

 $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

Then After Milo Remark, I discovered that I should never have to perform the default query myself in the template, the main query happens before the template is loaded. Then I was needing a way to inspect the $wp_query global to see why the main query is failing. The way to do so is to:

 echo '<pre dir="ltr">';
  echo '</pre>';

Result is:

[query_vars] => Array
            [tag] => alaa
            [taxonomy] => tag
            [term] => alaa

As you can see here, I have 'alaa' as my term name and also as my post tag. It comes from the fact that I am defining a custom taxonomy with the name 'tag', which gives the same url for 2 different things. In other words, blog/tag/alaa will refer to archive of default posts with tag 'alaa' and will refer to archive of custom-post-type posts with custom taxonomy 'tag' and term name 'alaa'.

So I changed my custom taxonomy name while registering from 'tag' to 'othername'.

Now, Wordpress consider blog/tag/alaa and blog/othername/alaa as two different things, naturally.

Note that, my changes didn't take effect till I clicked on save changes under permalinks settings without making any change (maybe to refresh rewrite rules).

  • 2
    You should never have to perform the default query yourself in the template, the main query happens before the template is loaded. Inspect the $wp_query global to see why the main query is failing.
    – Milo
    Jun 9 '13 at 14:19
  • That is what I know, but I really wasted a lot of time to make the main query work but to no avail. I will give a feedback after trying to inspect wp_query Jun 9 '13 at 14:23
  • Could you please tell me how can I inscpect wp_query? I am a beginner and I dont find how. I feel this technique is very useful. thanks in advance Jun 9 '13 at 14:42
  • I can now display $GLOBALS['wp_query'], but how can I know from this big array that my main query is failing? thank you for your time. Jun 9 '13 at 14:58
  • wrap the output in <pre> tags to make it more readable, compare the query vars and query being sent to the database with the custom query you perform above to see how they differ.
    – Milo
    Jun 9 '13 at 15:23

For custom post types, you'll need to hook pre_get_posts. By default the taxonomy pages only show posts of type post, page, and attachment.

// Ensure that our custom post types appear in the taxonomy archive pages
add_action('pre_get_posts', 'add_my_custom_post_type_to_taxonomy');
function add_my_custom_post_type_to_taxonomy($query) {
  if (empty($query->query_vars['suppress_filters'])) {
    if (is_tax('my_custom_taxonomy_type')) {
      $query->set('post_type', array('my_custom_post_type'));
  • Not necessary. Just register the post type and the taxonomy with proper arguments, and WordPress will show them automatically.
    – fuxia
    Dec 13 '15 at 7:18
  • I may have done something wrong then, because my custom post type wasn't being displayed on my custom taxonomy archive page.
    – thirdender
    Dec 13 '15 at 8:27
  • I just checked my code again and I'm still having an issue without my pre_get_posts action. What arguments am I missing? I used the custom post type and custom taxonomy generators on GenerateWP.
    – thirdender
    Dec 23 '15 at 6:06

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.