[wp-hackers] including the CPTs in the search result

Rocio Valdivia rocio at mecus.es
Wed Dec 7 07:52:11 UTC 2011


Hi IC IC,

I use the same function but with another filter, and it works fine for me
with all my proyects, some of them with heavy traffic (a spanish newspaper):

function searchAll( $query ) {if ( $query->is_search ) {$query->set(
'post_type', array( 'post', 'page', 'feed', 'custom_post_type1',
'custom_post_type2'));}return $query;}
add_filter( 'the_search_query', 'searchAll' );


If you want to exclude the pages of the search results, just delete 'page'
from the function ;)

-- 
-- 
---
Rocío Valdivia --- Mecus Consultores: http://mecus.es --- Twitter:
@_DorsVenabili


2011/12/7 IC IC <icwordpress at gmail.com>

> Is the following method technically sound and OK as a practice on a
> traffic heavy web site? Any more efficient ways you can recommend?
>
> function filter_search($query) {
>    if ($query->is_search) {
>        $query->set('post_type', array('post',
> 'custom_type_1_here','custom_type_2_here'));
>    };
>    return $query;
> };
> add_filter('pre_get_posts', 'filter_search');
>
> This will also make sure that the pages are excuded from the search
> result which is obviously optional.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list