I’m trying to exclude posts from a certain category from being displayed on my home.php.
The code that is in my theme is as follows:
query_posts(array('post__not_in' => $featured_posts_array));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="categories">
<h3><?php the_categories_excerpt(); ?></h3>
I tried adding the following before the query_posts ( function but it does nothing.
function exclude_category( $query ) {
if ( is_feed() ) {
$query = set_query_var( 'cat', '-1' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category' );
Is there some kind of format I need to follow?
First, don’t use
query_posts()
. Just get rid of the call entirely. It will break things.Second:
Callbacks and
add_action()
calls belong infunctions.php
, not in the template file. If you’ve put it directly inhome.php
, remove it from there, and put it infunctions.php
.Third:
Your
pre_get_posts()
filter uses theif ( is_feed() )
conditional. Theis_feed()
conditional returns true when an RSS feed is being output, not on the blog posts index (which is what is output viahome.php
). Try usingis_home()
instead.Fourth:
Don’t call
set_query_var()
inside your callback. Use$query->set()
instead.Putting it all together
Use the following in
functions.php
Questions
What category are you trying to exclude? Are you sure the ID is
1
?