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

Excluding Categories WordPress



I have a custom wordpress theme with a custom admin panel. There is an area where a user can type in which categories they want to hide. (ie: -5,-6,-7)

I am trying to pull in this value that they set and exclude those categories from the blog.

I know logically how to do it, but I'm having a bit of trouble.

On the blog page, I have modified "the loop" to the code below. That is how I need the final output to look. the only difference is that I am storing my values into a variable so I need I'm going to need format the loop a bit differently.

<?php query_posts(cat='-5,-6,-7''); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Logically this is what I need to do but it's not written correctly. Any help is greatly appreciated.

<?php $excludefull = get_option('nt_exclude'); ?>
<?php query_posts(cat='$excludefull'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Answers (1)


Lew Ayotte answers:

Is that the code you're using?

Chang query_posts to:

<?php query_posts('cat=' . $excludefull); ?>


WP Answers comments:

Thank you for that but it didn't work.

Would I need to add a closing quote ' so that it reads like: cat='-7'

in the code above there is no closing quote.

The value the the variable returns looks like this: -7,-6

Thanks a lot for your help.

Lew Ayotte comments:

$excludefull should be a string, so there is no need to quote it...

What displays when you do this:

<?php echo $excludefull; ?>


WP Answers comments:

Ah, ok I understand.

Here's what displays: -7,-3

Lew Ayotte comments:

Hrm... that should work then... you're sure that the category IDs are 7 and 3?

What happens when you try this as a test?

<?php query_posts('cat=-7,-3'); ?>


WP Answers comments:

Your Code was perfect.

i just wfound out whats causing the issue.

In my functions.[h[ file I had some code to help exclude categories from the archives widget.

Any chance you know how to modify this code from the functions.php file so that it excludes those same categories from widgets as well?

function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '-7,-3');
return $query;
add_filter('pre_get_posts', 'exclude_category');

Lew Ayotte comments:

It would probably depend on the widget and if the widget is part of WP Core... and if there are any action hooks for that widget.