jump to navigation

Excluding some WordPress posts or articles during searching July 11, 2011

Posted by Tournas Dimitrios in Wordpress - 3.

The search field on a WordPress Blog is very handy to search across the entire website to find articles that contain specific “keywords” .  But  you may need to exclude some static pages to be included during searching , for example : the “abaut-me” ,  ” my-services” , “portfolio” pages etc….  You just need to put the following code in functions.php file , this file is in your wordpress theme folder ( Dashboard -> Appearance -> Editor -> Select your Theme from Drop-down-menu ->  functions.php ) . The array in the script holds all post/article ID’s to be excluded from searching . To find the ID’s read this article .  Paste the code at the end of the file and don’t forget to press “Update File ” .

// Remove some post - pages from search

function custom_filter($query) {
// If not administrator and if search in progress
 if ( !$query->is_admin && $query->is_search) {
// ID's of pages/posts
 $query->set('post__not_in', array(4, 210,62,64) );
 return $query;
add_filter( 'pre_get_posts', 'custom_filter' );


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s