Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>
              </a>
          </div>

        <?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.

share|improve this question
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 –  whiteletters and blankspaces Jun 9 '13 at 23:42

2 Answers 2

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.

share|improve this answer
    
Thank you @s_ha_dum but my post type is already registred with 'public' => true. –  whiteletters and blankspaces 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. –  whiteletters and blankspaces Jun 9 '13 at 20:30
up vote 1 down vote accepted

Solution:

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

<?php

$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">';
    print_r($GLOBALS['wp_query']);
  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).

share|improve this answer
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 –  whiteletters and blankspaces 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 –  whiteletters and blankspaces 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. –  whiteletters and blankspaces 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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