If you register the post type with public
set to true
, the titles will be included automatically. You shouldn’t have to do anything to make this happen. Something as simple as this from the Codex:
function codex_custom_init() {
$args = array( 'public' => true, 'label' => 'Books' );
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );
If you aren’t sure you can test it with this:
function test_custom_types_inclusion($where) {
if (is_search()) {
wp_die(var_dump($where));
}
}
add_filter('posts_where','test_custom_types_inclusion');
That will completely break you search but you can see the raw WHERE
clause to confirm that your CPT is included. Look for the post_type IN (
part.
That covers this question:
is there any way to include custom post title as search term[?]
But I think you meant that you want to search the names you use to register the types, not the titles. To include all posts listed in a custom post type– and I don’t see the logic of doing that, as you are going to get a potentially huge number of results, making the search more or less useless– something like this…
function custom_types_inclusion_wpse_81742($where) {
if (is_search()) {
global $wpdb;
$where .= " OR {$wpdb->posts}.post_type="your-type-name"";
}
return $where;
}
add_filter('posts_where','custom_types_inclusion_wpse_81742');
But to have your search terms respected you will have to get more complicated.
function custom_types_inclusion_wpse_81742_v2($search) {
global $wpdb;
$terms = preg_match_all('/%([^%]+)%/',$search,$matches);
$ts = array();
if (!empty($matches[1])) {
$matches = array_unique($matches[1]);
if (!empty($matches)) {
foreach ($matches as $m) {
$ts[] = "{$wpdb->posts}.post_type LIKE '%{$m}%'";
}
}
if (!empty($ts)) {
$search .= "OR ((".implode(') OR (',$ts).'))';
}
}
return $search;
}
add_filter('posts_search','custom_types_inclusion_wpse_81742');
Untested but I think that should do it.
Again, I don’t see the logic. Results aren’t going to mean much.