X

Exclude a Custom Post Type From Search

Snippets by IsItWP

You may have all post types allowed to show in the search results. However, there are few custom post types like slider on the homepage that aren’t useful to appear in the searches, so you can simply exclude a custom post type from search.

Instructions: Add the following code to the functions.php file of your WordPress theme.


<?php
add_action('init', 'codex_custom_init');
function codex_custom_init()
{

  $args = array(
    'exclude_from_search' => false, // the important line here!
    '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','editor','author','thumbnail','excerpt','comments')
  );
  register_post_type('book',$args);
}
?>

You may also enjoy filtering search by users in WordPress.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!