Home / functions.php / Filter search results by post type
  • How to Filter search results by post type

    no thumbnail sorry: Filter search results by post type

    Adding this PHP code to the functions.php of your wordpress theme will only search posts, and custom post types, excluding pages.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function search_posts_filter( $query ){
        if ($query->is_search){
            $query->set('post_type',array('post','custom_post_type1', 'custom_post_type2'));
        }
        return $query;
    }
    add_filter('pre_get_posts','search_posts_filter');
    

    ( WordPress codex functions, hooks, in this snippet. )

    query, get_post, get_posts, add_filter, is_search,