m4k3r-net
8/18/2018 - 1:09 PM

Include All Custom Post Types In Wordpress Search

Include All Custom Post Types In Wordpress Search

<?php
function include_post_types_in_search($query) {
    if(is_search()) {
        $post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
        $searchable_types = array();
        if($post_types) {
            foreach( $post_types as $type) {
                $searchable_types[] = $type->name;
            }
        }
        $query->set('post_type', $searchable_types);
    }
    return $query;
}
add_action('pre_get_posts', 'include_post_types_in_search');
?>