Karen Doyle
asked 7 years ago

Hello
I’d like to know how can i remove the question and answer from the search results of my wordpress ?
Thanks

1 Answers
DominicStaff
answered 7 years ago

Hi,
You can add the following code to the functions.php file to make WordPress search only works with posts, pages:

function wpshock_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post','page') );
    }
    return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');

Hope this helps !

termoplus
replied 7 years ago

Great! You are the best!

In order to search only post I need to delete ‘page’ in array?

Thank you! You are the best!

termoplus
replied 7 years ago

Dominic, can you please me search only post without page?
Does this code affect only WP standard search?
I would like standard WP search should find post and search on your plugins page should find only questions.

thanks

dominic Staff
replied 7 years ago

You can try the following code:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

Hope this helps !

termoplus
replied 7 years ago

Great!
Still plugins search finds all posts too but I think this is a minor bug which will be resolved in the next release.

Powered by DW Question & Answer Pro