Search code examples
phpwordpresshttp-redirectwordpress-themingcategories

WordPress how to exclude certain categories via functions.php


I'm trying to exclude certain categories and all user archives
e.g.

From my WordPress-Site via functions.php, rerouting them to home.

This is what I've tried so far:

/* Route categories to home */
function reroute_wp_categories(){
  if( is_category( array('category1', 'category2', 'category3' )) || is_author() ) {
    global $wp_query;
    $wp_query->is_home(); //set to Home
  }
}

with category x being the category slug.

This doesn't work at all and I'm running out of ideas.

Grateful for any help/ideas.


Solution

  • This should do the trick. In your functions.php add the following

    function reroute_wp_categories( $query ) {
        if ( is_author() || is_category(array('category1', 'category2', 'category3' )) ) {
            wp_redirect(home_url());
            exit();
        }
    }
    add_action( 'pre_get_posts', 'reroute_wp_categories' );