WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't need to search for pages in my site and only want to search posts, is there a way to do it? Thanks

share|improve this question
up vote 18 down vote accepted

The below should make the page post type no longer search-able.

function remove_pages_from_search() {
    global $wp_post_types;
    $wp_post_types['page']->exclude_from_search = true;
add_action('init', 'remove_pages_from_search');
share|improve this answer
Where do I place that code? Thanks – Lisandro Dec 16 '10 at 3:09
You can place it in functions.php of your theme. – prettyboymp Dec 16 '10 at 3:18
Thanks an awful lot. I just copy pasted it and it works perfectly – Lisandro Dec 18 '10 at 20:14

The following in functions.php also works well:

//Remove pages from search results
function mySearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
return $query;
share|improve this answer
shuold be the sollution. more elegant, more maintainable. hte above just modifies post type params, this one the search query itself. +1 – kaiser Feb 4 '11 at 9:37
Modifying the post type object for the 'page' post type is a better solution. This solution forces the post type to be hard set as 'post' for all search queries which would keep any custom post types from showing up in the search if a site had any. Globally setting the exclude_from_search property of the 'page' post type object means that any code that depends on checking this property will have the correct information. – prettyboymp Jan 23 '12 at 18:44
Somewhat relevant link with a little more information: codex.wordpress.org/… – its_me Feb 27 '13 at 5:57
Works well but not shoing topic in bbpress, so it is not good. Using "$wp_post_types['page']->exclude_from_search = true;" is a good options seems – Kishore Sahoo Jul 24 '15 at 13:27

put this in your search.php

<?php if (is_search() && ($post->post_type=='page')) continue; ?>

right below this code -> <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>

you can find more on here http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages

share|improve this answer
I don't have a search.php if I retrieve one from another theme will it work? Thanks – Lisandro Dec 16 '10 at 3:10
yes. first create a blank search.php then copy and paste the code from index.php. then do what i told you above. – andrewk Dec 16 '10 at 5:33
actually you should use @prettyboymp's code. its easier and does what you asked. i tested it. if you want.. – andrewk Dec 16 '10 at 5:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.