Category Archives: PHP

How to exclude specific blog categories from blog archive page

Hey Friends,
If you want to remove or hide any specific blog categories from the blog archive page in WordPress. So you just need to add the below function or hook in function.php file in your theme folder.


   function exclude_category( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {
   $query->set( 'cat', '-1, -2' ); //The IDs of the categories to exclude.
   }
   }
   add_action( 'pre_get_posts', 'exclude_category' );