0

Is there a way to use WP_Query's method, set(), in order to exclude a specific post_type from a global search.

Here was my unsuccessful attempt:

function _search_all($query) {  
if($query->is_search()) {
            // get rid of in the news
            $query->set('post_type__not_in', array(IN_THE_NEWS_SLUG));

    if(isset($_GET['post_type'])) {
        $query->set('post_type', $_GET['post_type']);
    } /*else {
        $query->set("post_type", array(FEATURES_ID, BRIEFS_ID, PRESS_RELEASES_ID));
    }*/
}
return $query;
}
add_filter("pre_get_posts", "_search_all");
6
  • I'd also like to point out that I cannot query all the post_types that I do want, because I use a faceted filter on the search page, and if I set all of them, they set all the faceted items checked, and by default I need them to remain unchecked before a user applies the filters.
    – cj5
    Nov 21, 2013 at 19:02
  • 1
    did you try to use the exclude_from_search => 'true' while registering the post_type Nov 21, 2013 at 19:21
  • @ChrisWalsh Hmm... Why do you think your code should work? Where did you find that post_type__not_in? I'm pretty sure that WP_Query has only post_type argument (string or array). Nov 21, 2013 at 19:47
  • 1
    @MarutiMohanty Thank you. I totally overlooked that setting.
    – cj5
    Nov 21, 2013 at 19:52
  • @KrzysiekDróżdż Let me point out to you my own words, "Here was my UNSUCCESSFUL ATTEMPT". =) I acknowledged that it did not work. Cheers!
    – cj5
    Nov 21, 2013 at 19:53

1 Answer 1

2

You can define the exclusion of post_type while registering for the post.

Use the below param to make this work

exclude_from_search => 'true'

For more details check the codex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.