Rufusmedia
6/5/2018 - 9:34 PM

Wordpress Post Exclusion by Category

// excludes posts with the category "No Home Page" from the home page article index
function exclude_category($query) {
    if ( $query->is_home() ) {
    $query->set( 'cat', '-555' );
    }
    return $query;
    }
    add_filter( 'pre_get_posts', 'exclude_category' );

// removes the "No Home Page" category link from the post where it appears on the site
    if(!is_admin()) {
        function exclude_these_categories($thelist, $separator=' ') {
            //Exclude the following categories
            $exclude = array('No Home Page');
            $cats = explode($separator, $thelist);
            $newlist = array();
            foreach($cats as $cat) {
                $catname = trim(strip_tags($cat));
                if(!in_array($catname, $exclude))
                    $newlist[] = $cat;
            }
            return implode($separator, $newlist);

    }
    add_filter('the_category','exclude_these_categories', 10, 2);
    }