Exclude Custom Post Type from Search

More often than not, content is added to a website for aesthetic purposes (carousels, widgets, call to action buttons, etc.), not necessarily to provide information.

The example below shows a Custom Post Type that was setup for rotating banner content. Carousels / Sliders will often show snippets of information, and link to other areas of the website. Therefor showing their titles and content in the Search Results can be confusing and frustrating to visitors; these single template views are (often) not themed to display the content as it would appear in the rotating banner, and will display nothing if linked to from a search result.

See below for information about where to add ‘exclude_from_search’ => true to activate this feature:

/***** Theme Customization *****/

add_action( 'init', 'create_post_type' );
function create_post_type() {
 
 $labels = array(
 'name' => _x('Banners', 'post type general name'),
 'singular_name' => _x('Banner', 'post type singular name'),
 'add_new' => _x('Add New Banner', 'Banner'),
 'add_new_item' => __('Add New Banner'),
 'edit_item' => __('Edit Banner'),
 'new_item' => __('New Banner'),
 'all_items' => __('All Banners'),
 'view_item' => __('View Banner'),
 'search_items' => __('Search Banners'),
 'not_found' =>  __('No Banners found'),
 'not_found_in_trash' => __('No Banner found in Trash'), 
 'parent_item_colon' => '',
 'menu_name' => __('Banner')
 );
 $args = array(
 'labels' => $labels,
 'public' => true,
 'publicly_queryable' => true,
 'show_ui' => true, 
 'show_in_menu' => true, 
 'query_var' => true,
 'rewrite' => true,
 'capability_type' => 'post',
 'has_archive' => true, 
 'hierarchical' => false,
 'menu_position' => null,
 'supports' => array( 'title', 'author', 'thumbnail', 'revisions', 'editor', ),
 'exclude_from_search' => true, // Add this to the args to exclude the CPT from search results
 ); 
 register_post_type('banner',$args);
}

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.