A fresh mix of code snippets and tutorials

Exclude pages from WordPress search results

By default WordPress includes all of your blog’s pages in its internal search results.

But sometimes you may want to display only posts and hide the pages. So what are you gonna do? Easy. Just add the following code to your theme’s functions.php file:

function filter_search($query) {
	if ($query->is_search) {
		$query->set('post_type', 'page');
	return $query;
add_filter('pre_get_posts', 'filter_search');

Similarly, you could modify this function to exclude/include just about anything you wish. For example, page is just one many different post_types that WordPress makes available. Visit the WP Codex for more ideas.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress