How to show only post-types on search results?

banner-image

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.

Tags:

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x