Your problem is your user request. It is very important to note here that never change the main request using a custom archive on any type of page or on the home page. I have explained in detail everything in this post recently. Be sure to read it and all related messages, as this will be of great benefit to you.
Your solution would be to remove your user request and replace it 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 request, use pre_get_posts for this
EDIT
Your best idea here would be to use full tax_query to display posts that are in the selected taxonomy and tag
You can try something like this: (At least PHP 5.4+ is required. Also, this has not been verified)
$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 versions of PHP, 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 messages that are in the sector1 tag and any other sectorX tag, you can do the following
You can try something like this: (At least PHP 5.4+ is required. Also, this has not been verified)
$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 versions of PHP, 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 notice, you can pass an array of tags to a terms parameter like this
'terms' => array( 'sector1', 'sector2', 'etc' ),
or syntax for short arrays
'terms' => ['sector1', 'sector2', 'etc'],
EDIT 3
Since this is your main request, you need to make a few changes. As I said, delete the user request. 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 -->
Now you can use pre_get_posts to remove the desired tag from your taxonomic pages. In your functions.php, follow these steps: (Requires PHP 5.3+, and also not verified)
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 ) ); } }
Remember to change 145 to your exact id tag or ids tag array
EDIT 4
If you do not have tag identifiers, you can use get_term_by() to get the tag identifier from the tag. Something like this will do: (Requires PHP 5.3+, and also not verified)
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 tags, 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 ) );/*
from
$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 );
Remember to change bullets correctly.
EDIT 5
Your last 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 ); } }