banner-image

How to show only post-types on search results?

Usually, when we search something on the default WordPress search form, it shows all posts and pages related to that search results.

But by default, the search form did not shows the post types related to that search query.

Please use the below function on your active theme’s functions.php file to display post-types within search results as well.

//Add this to this functions.php file
add_filter( 'pre_get_posts', 'tgm_cpt_search' );
function tgm_cpt_search( $query ) {
    if ( $query->is_search )
        $query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio' ) ); //post types
    return $query;
};

 

For more knowledge on this, please visit this link.

Do You Want To Boost Your Business?

drop us a line and keep in touch