Skip to content

Instantly share code, notes, and snippets.

@tdmrhn
Last active July 20, 2022 17:39
Show Gist options
  • Save tdmrhn/68107a250bdc6ada552f6b80bc5aac84 to your computer and use it in GitHub Desktop.
Save tdmrhn/68107a250bdc6ada552f6b80bc5aac84 to your computer and use it in GitHub Desktop.
Exclude Category from Blog Page
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1, -2, -3' );
}
return $query;
} );
@tdmrhn
Copy link
Author

tdmrhn commented Jun 28, 2022


// remove category from category list
add_filter( 'get_terms', function ( $terms, $taxonomies, $args ) {
	$new_terms = array();
	if ( is_home() ) {
		foreach ( $terms as $key => $term ) {
			if( is_object ( $term ) ) {
				$excluded_taxonomy = array ('places');  // for multiple taxonomy use comma
				if(in_array($term->slug, $excluded_taxonomy ) && $term->taxonomy == 'category') { // change taxonomy as you need
					unset($terms[$key]);
                }
            }
        }
    }
    return $terms;
}, 10, 3 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment