Hide Category from Feature News

  • Hello!

    I know in the theme settings you specify a category under Theme->Home News Category. Every post with that category will show there. What I’d like to do is hide posts from populating here if the user is not logged in. So these posts would have a second “hidden” category.

    I’ve tried the following in the functions.php which works great for the posts (news) page, but not the home featured news.

    function exclude_category( $query ) {
        if ( !is_user_logged_in() ) {
             if ( ( $query->is_home() || $query->is_feed() || $query->is_search() || $query->is_archive() ) && $query->is_main_query() ) {
                $query->set( 'cat', '-8' );
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Feel free to check out http://www.wnypa.org. On the home page the ‘picnic’ post is not suppose to show. The ‘live’ would be the only post to show.

    Thanks in advance!

    The blog I need help with is: (visible only to logged in users)

  • Hello,

    The features news posts on the home page use a custom query “$news”.

    You could try a similar function replacing “$query” with “$news”. However, this is untested.

    I’ve committed another theme update that adds a post class to those news posts. Meaning, each post will have multiple relevant classes applied, including the category classes. As a result, you may be able to hide the hidden category posts using CSS once the update goes live. This would likely be a cleaner solution than editing the PHP.

    Thanks!

  • Thanks David! I’ll keep an eye out for the update and try your solution with CSS.

    Appreciate the help!

  • You’re welcome!

  • The topic ‘Hide Category from Feature News’ is closed to new replies.