fbpx

Use the following snippets of code to remove all posts by category from different sections of your WordPress site.

Add this code to your theme’s function file being careful not to remove or edit any other code in there!

Hide posts with a certain category from your blog index page:

function blog_exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-5' ); //add multiple categories by separating with commas '-5,-6,-8' ...
}
}
add_action( 'pre_get_posts', 'blog_exclude_category' );

Hide category link from the sidebar widget:

function widget_exclude_category($args){
$exclude = "5"; // IDs of excluding categories add multiple categories by separating with commas '5,6,8' ...
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","widget_exclude_category");

Hide posts with a certain category from the recent posts widget in the sidebar:

function recentPostWidget_exclude_category() {
$exclude = array( 'cat' => '-5' ); //add multiple categories by separating with commas '-5,-6,-8' ...
return $exclude;
}
add_filter('widget_posts_args','recentPostWidget_exclude_category');

  • Check out these related articles

    Technical
    March 8, 2019

    Hide Post Category from blog and sidebars in WordPress

    Use the following snippets of code to remove all posts by category from different sections of your WordPress site. Add this code to your theme's function file being careful not…
    Technical
    January 9, 2019

    Create a single page PDF from a web page

    Worked out this little trick while trying to save an email (from MailChimp) into a single page PDF using Adobe Acrobat. There's a handy feature called Create PDF from Web…

    Join Our Community

    Enter your email below and sign up to our email list. You will be the first to know when we post a new article.

Leave a Reply