Hiding posts from main page/archive but not RSS feed

I’m trying to hide certain posts (either by ID or category) from my site’s main post list, archive post list, and the custom search feature, -but- I need those posts to still show up in RSS feed.

I’ve tried multiple approaches from articles/ChatGPT, but none have worked to hide the posts from the above locations. I’m stumped. It seems as if the add_action on pre_get_posts is just not working for some reason, but I’m not sure why.

Below are a few of the example code snippets I’ve tried in functions.php:

function myFilter($query) {
    if ($query->is_feed) {
        $query->set('cat','-1'); //Don't forget to change the category ID
return $query;

function exclude_posts_from_homepage($query) {
    if ($query->is_home() && $query->is_main_query()) {
        // Replace with the IDs of the posts you want to exclude from the homepage
        $exclude_posts = array(2324,2325,2327); // Replace with actual post IDs
        // Exclude posts from the homepage
        $query->set('post__not_in', $exclude_posts);
add_action('pre_get_posts', 'exclude_posts_from_homepage', 1);


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer