WordPress – Remove pages from search results

To remove the pages from the search results, use the code in this article.

/* Remove pages from search results */
function SearchFilter($query) {
	// Check if query is search
	if ($query->is_search) {
		// Add the wanted post type
		$query->set('post_type', 'post');
	}
	
	// Return the query
	return $query;
}

add_filter('pre_get_posts','SearchFilter');Code language: PHP (php)