[Resolved] Excluding Specific Tag or Page from Search

Home Forums Support [Resolved] Excluding Specific Tag or Page from Search

Home Forums Support Excluding Specific Tag or Page from Search

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1037569
    yig

    Hi there!

    Is it possible to exclude a specific tag from wordpress search results? Also, how would it be possible to exclude specific pages from the results?

    Is there a specific code for these so I can add to the functions.php file?

    Thanks in advance!

    #1037597
    Leo
    Staff
    Customer Support

    Hi there,

    Just to make sure, are you referring to WordPress search widget or GP’s navigation search?

    Let me know πŸ™‚

    #1037615
    yig

    Hi Leo,

    Thanks for your response. I actually meant the GP navigation search (The one I enabled from the primary navigation menu)

    I thought the one on the Off Canvas Panel was the same, but you made me realise I have it as a separate widget there. Maybe I’ll just remove that one πŸ™‚

    #1037706
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This article might help: https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/

    Specifically:

    add_filter( 'pre_get_posts', function( $query ) {
        if ( $query->is_search && ! is_admin() )
            $query->set( 'tag', '-19' );
        }
    
        return $query;
    } );
    #1037828
    yig

    Thanks for your response Tom!

    Actually I had tried that article before asking here but it gave me a functions.php error, so I thought maybe a different code is necessary for Generatepress. If that code works for you maybe it was about a plugin I use (Page Tags).

    There are specific pages I wanted to exclude from the search.

    As a workaround I created a new author, made it the author for these specific pages and excluded this author from the results (with the code from the same article). It worked well both for the widget and the nav search.

    function wpb_search_filter( $query ) {
        if ( $query->is_search && !is_admin() )
            $query->set( 'author','-24' );
        return $query;
    }
    add_filter( 'pre_get_posts', 'wpb_search_filter' );
    #1038328
    Tom
    Lead Developer
    Lead Developer

    Nice workaround! Very similar solution πŸ™‚

    #1131536
    Nacho

    Hello,

    How would you exclude the GP search funcionanility from searching pages?

    Thanks a lot.

    #1132054
    Leo
    Staff
    Customer Support

    Any chance you can open a new topic for your question?

    Thanks πŸ™‚

    #1132664
    Nacho

    Sure!

    #1133111
    Leo
    Staff
    Customer Support

    Thanks πŸ™‚

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