• Resolved savingyourtail

    (@savingyourtail)


    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;
    }
    
    add_filter('pre_get_posts','myFilter');
    
    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);
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Either or both approaches are valid. However, you’ve introduced some minor errors preventing success.

    In the exclusion by category, pre_get_posts is an action hook, not a filter. Thus there is no need to return a value and you should use add_action(), not add_filter(). That’s not the real issue though. The real issue is you’ve excluded category 1 from feeds, not your home page. You need to negate is_feed like so: ! $query->is_feed

    In the exclude by ID, you’ve neglected to include ! $query->is_feed as one of the conditions. Also, you are passing 1 as the $priority arg for add_action(). I assume you are trying to give your callback the highest priority. To do that, the arg should be a very large number so your callback has the final say in the matter.

    Thread Starter savingyourtail

    (@savingyourtail)

    Thanks for the suggestions. I’ve incorporated them all below. I tried both approaches, exclusion by category ID and exclusion by post ID, but unfortunately the posts are still visible even with these methods. (Caches were cleared & post ID’s and category ID’s double checked). What else could the problem be? Below is the updated code.

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

    add_action('pre_get_posts','myFilter');
    function exclude_posts_from_homepage($query) {
    if ($query->is_home() && $query->is_main_query() && ! $query->is_feed) {
    // 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', 99999);
    Thread Starter savingyourtail

    (@savingyourtail)

    I figured out the issue, or at least a work-around. I have a custom theme and there is custom shortcode buried in functions.php that generates the post lists. Placing the following code in the while loop that generates the posts solved the problem and now posts are being hidden as desired.

                while($query->have_posts()) :
                     $query->the_post() ;
                    $post_ids = get_the_ID() ;
                    $exclude_ids = array(2324,2325,2327);
                    if(!in_array($post_ids, $exclude_ids)){
    Moderator bcworkz

    (@bcworkz)

    If a shortcode handler is making the query, pre_get_posts could still work, but you’d need to be able to identify that specific query from all the others. For example, is_main_query() would not be a valid check for such a query.

    Since the shortcode belongs to custom theme, ideally you’d directly modify the query’s args before it is instantiated. I think this might be what you’re doing now, but you’ve not provided enough context for me to be sure.

    Even that’s not what you’re doing here, there’s always the “if it’s not broken, don’t fix it” rule ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.