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

  • SOLVED

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)

2010-04-23

Milan Petrovic answers:

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

<?php

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

?>


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

2010-04-23

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.

<strong>UPDATE</strong>
Ok then

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

?>

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 http://www.siddatwork.com/projects/test/?m=201003
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.