Filter front page posts by category

I have three categories (news, pics, vids), but I have all of them showing up on my front page. I would just like the front page to have the news category (and possibly uncategorized category) to show. What is the best way to do this? Plugin? Or a piece of code on the front page?

Related posts

Leave a Reply

1 comment

  1. Try adding this code to functions.php file:

    add_action('pre_get_posts', 'ad_filter_categories');
    function ad_filter_categories($query) {
        if ($query->is_main_query() && is_home()) {
            $query->set('category_name','news, uncategorized');
        }
    }
    

    category_name is the slug or the nicename of the category. Add a comma separated list of the categories you wish to include.