Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Exclude homepage and pages/posts without content from search results

  • Hello,

    Is it possible to exclude the homepage and pages/posts without any content from the search results? If so, how?

  • Hi there,

    It’s possible with a snippet:

    function exclude_posts_and_pages_from_search($query) {
        // Check if this is the main query and it is a search query
        if ($query->is_main_query() && $query->is_search()) {
            // Exclude specific post and page IDs from search results
            $excluded_ids = array(1, 2, 3); // Add the IDs of the posts and pages you want to exclude
    
            // Modify the query to exclude the specified posts and pages
            $query->set('post__not_in', $excluded_ids);
        }
    }
    
    // Hook the function to the pre_get_posts action
    add_action('pre_get_posts', 'exclude_posts_and_pages_from_search');

    Replace 1, 2, 3 with the IDs of the pages and posts you want to prevent from showing.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Getting the Page/Post ID: https://kinsta.com/blog/wordpress-get-post-id/

  • Fixed, thank you!

  • You’re welcome!

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