Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action(
- 'pre_get_posts',
- function ( $q ) {
- if ( is_admin() ) {
- return;
- }
- $post_type = $q->get( 'post_type' );
- if ( empty( $post_type ) ) {
- $post_type = 'post';
- }
- if ( is_array( $post_type ) || 'post' !== $post_type ) {
- return;
- }
- $excluded_cat_id = 1; /* Change this with your exclulded category ID */
- $cat_in = $q->get( 'category__in' );
- if ( ! empty( $cat_in ) ) {
- if ( ! is_array( $cat_in ) ) {
- $cat_in = array( $cat_in );
- }
- $cat_in = array_map( 'intval', $cat_in );
- if ( in_array( intval( $excluded_cat_id ), $cat_in, true ) ) {
- return;
- }
- }
- $existing = $q->get( 'category__not_in' );
- if ( empty( $existing ) ) {
- $existing = array();
- } elseif ( ! is_array( $existing ) ) {
- $existing = array( $existing );
- }
- $existing = array_map( 'intval', $existing );
- $existing[] = $excluded_cat_id;
- $merged = array_values( array_unique( $existing ) );
- $q->set( 'category__not_in', $merged );
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment