I have this code in my index.php file. I have a different template for a static home page, this is the blog page. I’m trying to exclude all posts with the category “new” which is tag_id “13”
<?php query_posts($query_string . '&cat=-13'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><p class='lead'><?php the_title(); ?></p></a>
<p><?php the_excerpt(); ?> <a href="<?php the_permalink()?>">read in full</a></p>
<p class='muted'><small>Written by: <?php the_author_posts_link(); ?><br>
<?php the_time('F jS, Y') ?></small></p><hr>
<?php endwhile; ?>
Any ideas why this isnt working?
Don’t use
query_posts()
. Usepre_get_posts
instead:This callback will exclude category 13 from the main loop in the blog posts index.
The
$query_string
is probably not initialized or not declared as global variable. Try addingbefore your code
I was running into the exact same mysterious issue, but struggling to solve it. I tried all of the suggestions in the comments here but nothing seemed to work.
In the end, since the key was retaining the pagination (as this was on the main blog section of the site and I wanted to exclude ‘Other News’), I tried this:
…and this seemed to work perfectly.