Technically, pre_get_posts
is an action, not a filter, and you don’t need to return anything and $query
is passed in by reference, but that code should nonetheless work. Cleaned up it would be:
function excludeCat($query) {
if ( $query->is_home ) {
$catid = get_cat_ID('watch-isatv');
$query->set('cat', '-'.$catid);
}
}
add_action('pre_get_posts', 'excludeCat');
There are several potential points of failure though.
- You are adding the filter after the main query has run. You must add
that filter before the main query.functions.php
should work but
adding that code in your other theme template files, like
archive.php
orhome.php
won’t. The main query has already executed. $query->is_home
isn’t what you expect or need it to be. Add
var_dump($query->is_home);
to your code and load your page(s).
Make sure it istrue
where you need it to be andfalse
otherwise.- The slug, not the human readable name, used with
get_cat_ID()
is
incorrect.
I’d put my money on #1 being the problem.