Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Leave pages out of search results

Site search only looks through posts (and products, if you have a shop), not pages like Contact or Privacy policy.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Category Search and archives
  • Tested with WordPress 7.1
PHP
<?php

add_action(
	'pre_get_posts',
	static function ( $query ) {
		if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || $query->get( 'post_type' ) ) {
			return;
		}
		$types = get_post_types( array( 'exclude_from_search' => false ) );
		unset( $types['page'], $types['attachment'] );
		$query->set( 'post_type', array_values( $types ) );
	}
);

In these packs

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.