Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 12 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
add comment

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;
}
add_filter('pre_get_posts','mySearchFilter');
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
add comment

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
1  
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
add comment

Your Answer

 
discard

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