$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Tag.php not displaying posts with the tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Tag.php not displaying posts with the tag

matteradmin9PV0评论

Ive got a CPT and in my CPT's argument I am calling the taxonomy post_tag. When I create a tag.php file it doesn't show any of the posts for said tag. My tag.php:

<?php get_header(); ?>

    <section class="row">
        <div class="col-md-7">
            <p>Tag: <?php single_tag_title(); ?></p>
            <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
                <h1><?php the_title(); ?></h1>
            <?php endwhile;     
            else: ?>
                <p>not working</p>          
            <?php endif; ?>             
        </div>
        <?php get_sidebar(); ?>
    </section>

<?php get_footer(); ?>

In my research I ran across "tag.php doesn't work with tags on a custom post type post?" but I am using what I thought was the default for tags post_tag. When I was referencing WP_Query() Tag Parameters it doesn't show how to take into consideration for the tag clicked. When I search for tag.php I get Tag Templates and it doesn't show any examples that take into consideration all tags. What is the proper way to write a WP_Query() for all posts pertaining to the tag? I did run across wp_get_post_tags() after some research and reading "Wordpress get related pages (tag based) - wp_get_post_tags" but I'm not understanding the rewrite for tag.php and the codex has no examples. So how can I properly write my tag.php to return all posts for the clicked tag?

Ive got a CPT and in my CPT's argument I am calling the taxonomy post_tag. When I create a tag.php file it doesn't show any of the posts for said tag. My tag.php:

<?php get_header(); ?>

    <section class="row">
        <div class="col-md-7">
            <p>Tag: <?php single_tag_title(); ?></p>
            <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
                <h1><?php the_title(); ?></h1>
            <?php endwhile;     
            else: ?>
                <p>not working</p>          
            <?php endif; ?>             
        </div>
        <?php get_sidebar(); ?>
    </section>

<?php get_footer(); ?>

In my research I ran across "tag.php doesn't work with tags on a custom post type post?" but I am using what I thought was the default for tags post_tag. When I was referencing WP_Query() Tag Parameters it doesn't show how to take into consideration for the tag clicked. When I search for tag.php I get Tag Templates and it doesn't show any examples that take into consideration all tags. What is the proper way to write a WP_Query() for all posts pertaining to the tag? I did run across wp_get_post_tags() after some research and reading "Wordpress get related pages (tag based) - wp_get_post_tags" but I'm not understanding the rewrite for tag.php and the codex has no examples. So how can I properly write my tag.php to return all posts for the clicked tag?

Share Improve this question edited May 23, 2017 at 11:33 CommunityBot 1 asked Sep 20, 2015 at 18:13 user9447user9447 1,7927 gold badges30 silver badges55 bronze badges 1
  • 2 Use pre_get_posts to add your cpt to the main query on tag pages – Pieter Goosen Commented Sep 20, 2015 at 18:21
Add a comment  | 

2 Answers 2

Reset to default 9

I figured it out thanks to Pieter's comment:

In functions.php I added:

function tag_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_tag) {
      $query->set('post_type', array( 'custom_post_type', ));
    }
  }
}
add_action('pre_get_posts','tag_filter');

This worked perfectly for me. I also wanted to include other post types, so I added:

$query->set('post_type', array( 'custom_post_type', 'post', 'page' ));

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far