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
5 Answers
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');
-
Where do I place that code? Thanks– LisandroDec 16, 2010 at 3:09
-
1
-
Thanks an awful lot. I just copy pasted it and it works perfectly– LisandroDec 18, 2010 at 20:14
-
Is there any way to hide specific pages instated of all the pages? Oct 18, 2021 at 11:19
-
This idea doesn't follow WP API and as any direct vars modification should be considered as hack rather than as solution. Oct 20, 2021 at 21:26
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');
-
shuold be the sollution. more elegant, more maintainable. hte above just modifies post type params, this one the search query itself. +1– kaiserFeb 4, 2011 at 9:37
-
2Modifying 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. Jan 23, 2012 at 18:44
-
Somewhat relevant link with a little more information: codex.wordpress.org/…– its_meFeb 27, 2013 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 Jul 24, 2015 at 13:27
Here is code for removing specific pages from search in WordPress, add the following code in a function.php file Just replace the array with your page ID's
function remove_pages_from_search($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post__not_in', array(3031,2958,2926));
}
}
}
add_action('pre_get_posts','remove_pages_from_search');
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
-
I don't have a search.php if I retrieve one from another theme will it work? Thanks– LisandroDec 16, 2010 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.– andrewkDec 16, 2010 at 5:33
-
1actually you should use @prettyboymp's code. its easier and does what you asked. i tested it. if you want..– andrewkDec 16, 2010 at 5:38
This solution follows (respects) WP API rather than modifying internal vars directly.
Notice that this way you can also include (overwrite registration) types or change registration arguments provided in register_post_type()
. Check out 'exclude_from_search'
description in docs.
/**
* Filter object types in search results.
*
* @param array $args Array of arguments for registering a post type.
* See the register_post_type() function for accepted arguments.
* @param string $type Post (object) type key (e.g. 'post', 'page', 'attachment'...,
* or any custom post type registered).
* @return array Filtered type attributes.
*/
function _filter_search_types( $args, $type ) {
$exclude_types = array( 'page' );
if ( in_array( $type, $exclude_types, true ) ) {
$args['exclude_from_search'] = true;
}
return $args;
}
if ( ! is_admin() ) {
add_filter( 'register_post_type_args', __NAMESPACE__ . '\_filter_search_types', 10, 2 );
}
-
@Rup - I said about WP API, not WP REST API (which is irrelevant here). And this is not "alternative way" of the accepted answer but "the right way", since "accepted answer" doesn't follow WP API and as any direct vars modification should be considered as hack rather than as a solution. Oct 20, 2021 at 22:51
-
@Rup - And you're right -
comment
isn't a post type (corrected). Oct 20, 2021 at 22:59 -