Displaying Tags on taxonomy page Displaying Tags on taxonomy page wordpress wordpress

Displaying Tags on taxonomy page


Your problem is your custom query. One very important note here is, never ever change replace the main query with a custom one on any type of archive page or the home page. I have explained everything in detail in this post recently. Make sure to read it and all the linked posts as this will benefit you a lot

Your solution would be to remove your custom query and replace this with the default loop that we all know

if ( have_posts() ) {    while ( have_posts() ) {        the_post();        // Your template tags and html mark up    }}

If you need to change anything in the main query, use pre_get_posts to do so

EDIT

Your best idea here would be to use a full tax_query to display posts that is in the selected taxonomy term and tag

You can try something like this: (Requires at least PHP 5.4+. Also, this untested)

$q = get_queried_object();$args = [    'post_type' => 'sectors',    'tax_query' => [        [            'taxonomy' => $q->taxonomy,            'terms' => $q->term_id,            'include_children' => false // Exclude child terms        ],        [            'taxonomy' => 'post_tag',            'field' => 'slug',            'terms' => 'sector1', //I believe this is the slug        ],    ],];

For older PHP versions, use the following

$q = get_queried_object();$args = array(    'post_type' => 'sectors',    'tax_query' => array(        array(            'taxonomy' => $q->taxonomy,            'terms' => $q->term_id,            'include_children' => false // Exclude child terms        ),        array(            'taxonomy' => 'post_tag',            'field' => 'slug',            'terms' => 'sector1', //I believe this is the slug        ),    ),);

EDIT 2

To exclude posts that are in the sector1 tag and any other sectorX tag, you can do the following

You can try something like this: (Requires at least PHP 5.4+. Also, this untested)

$q = get_queried_object();$args = [    'post_type' => 'sectors',    'tax_query' => [        [            'taxonomy' => $q->taxonomy,            'terms' => $q->term_id,            'include_children' => false // Exclude child terms        ],        [            'taxonomy' => 'post_tag',            'field' => 'slug',            'terms' => 'sector1', //I believe this is the slug            'operator' => 'NOT_IN'        ],    ],];

For older PHP versions, use the following

$q = get_queried_object();$args = array(    'post_type' => 'sectors',    'tax_query' => array(        array(            'taxonomy' => $q->taxonomy,            'terms' => $q->term_id,            'include_children' => false // Exclude child terms        ),        array(            'taxonomy' => 'post_tag',            'field' => 'slug',            'terms' => 'sector1', //I believe this is the slug            'operator' => 'NOT_IN'        ),    ),);

Just note, you can pass an array of tags to the terms parameter like this

'terms' => array( 'sector1', 'sector2', 'etc' ),

or short array syntax

'terms' => ['sector1', 'sector2', 'etc'],

EDIT 3

As this is your main query, you need to make a few changes. As I have said, remove the custom query. Your main loop should look something like this

<?php if (have_posts()) : ?>     <?php while (have_posts()) : the_post(); ?>         <a href="<?php echo get_permalink(); ?>">         <?php echo "<div class='col-md-6 col-sm-6 col-xs-12' style='margin-bottom:30px;'>"; ?>         <div class="row mobilemargin">             <div class="categorytiletext2">                 <div class="col-md-6 col-sm-12 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'hovereffect newimgheight')); ?> </div>                 <div class="col-md-6 col-sm-12 col-xs-12 mobilewhite">                     <div class="testdiv">                         <h5 class="captext"><?php the_title(); ?></h5>                         <?php $trimexcerpt = get_the_excerpt();                         $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' );                         echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';                         ?>                     </div>                 </div>             </div>         </div>         <?php echo "</div>"; ?>         </a>         <!-- If there is no posts, display an error message -->     <?php endwhile; else: ?>     <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?> <!-- If there is no posts, display an error message -->

You can now use pre_get_posts to remove the desired tag from your taxonomy pages. In your functions.php, do the following: (Requires PHP 5.3+, and is also untested)

add_action( 'pre_get_posts', function ( $q ){    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {       $q->set( 'tag__not_in', array( 145 ) );    }});

For older versions use

add_action( 'pre_get_posts', 'so30256167_remove_tags' );function so30256167_remove_tags( $q ){    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {       $q->set( 'tag__not_in', array( 145 ) );    }}

Just remember to change 145 to your exact tag id or an array of tagids

EDIT 4

If you don't have the tag ids, you can use get_term_by() to get the tag id from the tag slug. Something like this will do: (Requires PHP 5.3+, and is also untested)

add_action( 'pre_get_posts', function ( $q ){    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {        $tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );         $tagID = $tag_object->term_id;        $q->set( 'tag__not_in', array( $tagID ) );    }});

For older versions use

add_action( 'pre_get_posts', 'so30256167_remove_tags' );function so30256167_remove_tags( $q ){    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {        $tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );         $tagID = $tag_object->term_id;        $q->set( 'tag__not_in', array( $tagID ) );    }}

If you have an array of tag slugs, you can replace the following

$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' ); $tagID = $tag_object->term_id; $q->set( 'tag__not_in', array( $tagID ) );/*

with

$tag_array = array( 'slug1', 'slug2', 'slug3' );foreach ( $tag_array as $tag ) {    $tag_object = get_term_by( 'slug', $tag, 'post_tag' );     $tagids[] = $tag_object->term_id;} $q->set( 'tag__not_in', $tagids );

Just remember to change the slugs accordingly

EDIT 5

Your final code in functions.php with pre_get_posts should be

add_action( 'pre_get_posts', 'so30256167_remove_tags' );function so30256167_remove_tags( $q ){    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {        $tag_array = array( 'sector1', 'sector2', 'sector3', 'sector4' );        foreach ( $tag_array as $tag ) {            $tag_object = get_term_by( 'slug', $tag, 'post_tag' );             $tagids[] = $tag_object->term_id;        }         $q->set( 'tag__not_in', $tagids );        }}