Hide categories from WordPress

This method allows certain categories and their posts to be hidden from a wordpress site. It allows such posts to only be accessible using a direct link.

The filter was applied using a custom plugin. Using this method means that it will not be overwritten by any theme or other upgrades. A certain blog category is excluded using the category id. The plugin contains three function which exclude posts from various sections of the WordPress site.

Recent posts

The code below excludes posts from the recent posts feed on the site.

function exclude_posts_from_recentposts_widgets_by_categories() {
    global $categoriesToExclude;
    $categoriesToExcludeLength = count($categoriesToExclude);

    $categoriesToExcludeArray="";
    for($x = 0; $x < $categoriesToExcludeLength; $x++)
    {
       $categoriesToExcludeArray.= "-".$categoriesToExclude[$x]."";

       if($x == $categoriesToExcludeLength-1){$categoriesToExcludeArray.="";}
       else{ $categoriesToExcludeArray.=",";}
    }    
    $exclude = array( 'cat' => $categoriesToExcludeArray);
    return $exclude;
}
add_filter('widget_posts_args','exclude_posts_from_recentposts_widgets_by_categories');

Category widget

The code below excludes posts from the category widget feed on the site.

function exclude_cat_from_cat_widget($args){
    global $categoriesToExclude;
    $categoriesToExcludeLength = count($categoriesToExclude);

    $categoriesToExcludeArray="";
    for($x = 0; $x < $categoriesToExcludeLength; $x++)
    {
       $categoriesToExcludeArray.= "".$categoriesToExclude[$x]."";

       if($x == $categoriesToExcludeLength-1){$categoriesToExcludeArray.="";}
       else{ $categoriesToExcludeArray.=",";}
    }

    $exclude = $categoriesToExcludeArray;
    $args["exclude"] = $exclude;
    return $args;
}
add_filter("widget_categories_args","exclude_cat_from_cat_widget");

Main Feed The code below excludes posts from the main feed on the site.

function exclude_categories_posts( $query ) {
    global $categoriesToExclude;      
        $exclude = $categoriesToExclude;
        $exclude = apply_filters( 'exclude_categories_posts_filter', $exclude );
        // If the category archive is being displayed make an exception for that particular category
        if  ( is_category( $exclude ) ){
            $cat = get_category( get_query_var( 'cat' ) );
            $exclude = array_diff( $exclude, array( $cat->cat_ID, $cat->name, $cat->slug ) );
        }
        $query->set( 'category__not_in', $exclude );
        return $query;
}
add_filter( 'pre_get_posts', 'exclude_categories_posts' );

Next and Previous buttons The code below excludes posts from the next and previous button that appear as links on each blog post.

        return $join." INNER JOIN $wpdb->term_relationships ON 
                      (p.ID = $wpdb->term_relationships.object_id) 
                      INNER JOIN $wpdb->term_taxonomy ON 
                      ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";	
        $exclude = $categoriesToExcludeArray;
        $exclude = apply_filters( 'exclude_cat_from_previous_next_WHERE_filter', $exclude );
        return $where." AND $wpdb->term_taxonomy.taxonomy = 'category' 
                        AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";

Creating your first programming language is easier than you think,
...also looks great on your resume/cv.