Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Exclude Posts from Archive That Contain a Certain Tag WordPress


I want to prevent posts from displaying in my archives if the post has a certain tag assigned to it. How can I do this? Thanks.

Answers (2)


Milan Petrovic answers:

Open your theme archive.php or category.php template, and before the have_posts() call add this:


global $wp_query;
$wp_query->set("tag__not_in", array(723, 42));


instead of 723, 42 you can add list of your own tags to exclude. Tags must be stated as numbers using tags ID's (tag or term ID).


Sidd answers:

If you don't want to meddle with code then use the Simply Exclude plugin. That plugin will exclude posts based on tags from the archives.
The plugin can also exclude posts based on category.

Ok then

<?php global $wp_query;
$wp_query->set("tag__not_in", array(9));


Place this in the archive.php and before "the loop" which is before this <?php while (have_posts()) : the_post(); ?>

Here 9 is the id of the tag that i want to exclude.
Check out
Tag "people" is of ID 9 and gets excluded in the archive view but is visible on the homepage, for the post entitled "ex1"

Aaron comments:

Sorry, forgot to mention - I don't want a plugin solution, I would prefer it in the code/query.