Excluding category from blog

I’m trying to exclude the podcast category (25) from the blog I’ve added this code to my child theme functions php, but it isn’t working

function x_exclude_cat_from_blog($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set(‘cat’, ‘-25’);
}
}
add_action(‘pre_get_posts’, ‘x_exclude_cat_from_blog’); );

Hi there,

Thanks for writing in.

The code is added correctly in your child theme’s functions.php, but your child theme is not active. Please activate your child theme first.

And as confirmation, is it hiding podcast category or hiding posts under podcast category? Because that code is for hiding posts under the specific category ID.

Thanks!

I activated the child theme but now I get this error:
Parse error: syntax error, unexpected ‘)’ in /home/moder186/public_html/capturingmagic.me/wp-content/themes/x-child/functions.php on line 31

Got that fixed. There was an extra ); at the end.

You are correct, I want the posts from the Podcast category excluded from the blog. It’s working perfectly now.

Glad this is now sorted out!

For more information, please visit our Knowledge Base.

I was trying to use the same code and it’s not working.

function x_exclude_cat_from_blog($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set(‘cat’, ‘-78’);
}
}
add_action(‘pre_get_posts’, ‘x_exclude_cat_from_blog’);

It’s the category called Staff Profiles. Its showing up in the side bar on the News page. I want this category to show up in a page I’m building.

Hi There,

Thanks for writing in! We need your FTP access and WordPress credentials to check this issue. You can submit those information in a secure note.

In the meantime, you can also try adding the following CSS rule into your Theme Options > Global CSS area and should work.

.blog .category-staff-profiles {
    display: none;
}

Thanks!

Hello There,

Thanks for providing the url of the page in question. The code in the previous replies does not work for you and it does not apply to your site. The code above is for the blog index and not for the sidebar widget which is what you are having now. You will need a custom code that excludes a particular category from being displayed in a category widget in the sidebar. Please check out this thread as a reference to be able to achieve and exclude a category from your sidebar widget. Please check it here: https://wordpress.stackexchange.com/questions/192051/hide-specific-categories-from-category-widget

Hope this helps. Kindly let us know.

The code below actually worked to remove the blog from the feed, but you are correct that the sidebar is still showing it.

.blog .category-staff-profiles {
display: none;
}

I tried adding the code to the functions.php file and its still showing up in the sidebar.

Hello There,

The main content and the sidebar are two separate loops in displaying post items. The main content will display the recent blog items. This code will alter the main query.

function x_exclude_cat_from_blog($query) {
  if ($query->is_home() && $query->is_main_query()) {
    $query->set('cat', '-78');
  }
}
add_action('pre_get_posts', 'x_exclude_cat_from_blog');

This code cannot and will not include the sidebar because the sidebar widget category has an independent loop on its own. And that is why it is still displaying the category in the widget. You may add this code in your child theme’s functions.php file as well.

//Hide categories from WordPress category widget
function exclude_widget_categories($args){
    $exclude = "78";
    $args["exclude"] = $exclude;
    return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");

We would loved to know if this has work for you. Thank you.