[Resolved] Remove Specific Post From Blog Page

Home Forums Support [Resolved] Remove Specific Post From Blog Page

Home Forums Support Remove Specific Post From Blog Page

  • This topic has 14 replies, 3 voices, and was last updated 2 years ago by Ying.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #2259252
    Caitriona

    I am trying to remove a specific post from my blog page. How do I do it?

    #2259273
    David
    Staff
    Customer Support

    Hi there,

    try this PHP Snippet:

    function excludePostId($query) {
      $postIds = array(
        66
      );
    
      if ( $query->is_home() && $query->is_main_query() ) {
        set_query_var('post__not_in', $postIds);
      }
    }
    add_action('pre_get_posts', 'excludePostId');

    Change the 66 to the ID of the post you want to exclude.

    #2259331
    Caitriona

    Is there something missing in this code? I tried it by adding it to my Generatpress Child Theme and cleared the cache but I didn’t see any improvement.

    #2259354
    Ying
    Staff
    Customer Support

    Hi Caitriona,

    What’s the code you are using? Have you changed the 66 to the IDs of the posts?

    #2259365
    Caitriona

    Hi, yes I did. Sent you a screenshot.

    #2259391
    Ying
    Staff
    Customer Support

    The code looks right.

    I checked all 27 pages of your blog, I don’t see this post.

    #2259398
    Caitriona

    Did you try using the search bar? When I use the search bar I can see it. The search bar is in the top right when you click on Blog

    #2259401
    Ying
    Staff
    Customer Support

    That’s the search result page, your original request is to remove the post from blog.

    So do you want to remove the post from the search result page as well?

    #2259409
    Caitriona

    Ah ok. I didn’t know there would be a difference. Yes, I would like it too, please.

    #2259415
    Ying
    Staff
    Customer Support

    Then use this snippet instead:

    function excludePostId($query) {
      $postIds = array(
        4986
      );
    
      if ( ($query->is_home() ||$query->is_search() ) && $query->is_main_query() ) {
        set_query_var('post__not_in', $postIds);
      }
    }
    add_action('pre_get_posts', 'excludePostId');
    #2259437
    Caitriona

    Will this code remove from blog and search results? I need it removed from both. Or will I just use both snippets?

    #2259438
    Caitriona

    I have added it just now.

    #2259453
    Ying
    Staff
    Customer Support

    Only my snippet is needed, you can remove the old one.

    Just checked the search result page, it seems working well:
    https://www.screencast.com/t/IDEdVr9mjj

    #2259469
    Caitriona

    Thank you, Ying ๐Ÿ™‚

    #2260501
    Ying
    Staff
    Customer Support

    No problem ๐Ÿ™‚

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