• Resolved RayR75

    (@rayr75)


    Hi there,

    I’ve been searching for a while now, trying to find the correct way to exclude certain pages from my WP search results. However, what I keep finding online are outdated ways of doing so, like this:

    function ss_search_filter( $query ) {
        if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
            $query->set( 'post__not_in', array( 1, 2, 3 ) );
        }
    }
    add_action( 'pre_get_posts', 'ss_search_filter' );

    Is there someone who could give me an updated version of this. Or at least tell me what has changed since then? For now, I’ll keep searching. If I DO come across some new code, I’ll post it here.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code works for me on my site, once I use valid IDs for 1,2,3. Maybe something else is overriding yours? Try adding a large $priority arg so yours is set later.
    add_action( 'pre_get_posts', 'ss_search_filter', 9999 );

    Thread Starter RayR75

    (@rayr75)

    Thank you ‘bcworkz’, I forgot about an overwriting issue. This was indeed the case. There was some other code in the functions.php that caused my website to crash.

    It is solved now.

    Thanks for spending that time for me. Very much appreciated!

    Take care,

    Ray

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude specific pages from WP search results’ is closed to new replies.