0

A client has a website with a user directory and they'd like the user's author pages to be excluded from the site search so that if a user searches for "smith" they wouldn't get pages with "smith" as well as a link to John Smith's user profile.

I've added the following code to the functions file which is said to restrict search results to just posts and pages:

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

User profiles are still showing up in the search results, though. I've also echoed the global $wp_query and see that the post types are being set, but the author pages with a post_type of user (which isn't an actual post type) are still being displayed.

You can see in this image that post_type is set to posts and pages:

page and post are set Here, though, you can see that a post_type of user is still being pulled into the search results:

user post type still being shown

How should I go about excluding the users from the search?

Update: I've used the below code to output all of the registered post types to make sure that "user" isn't in the list.

add_action('init', 'update_my_custom_type', 99);
function update_my_custom_type(){
    global $wp_post_types;
    print_r($wp_post_types);
}
  • if "user" is not an actual CPT, how come you get a post with such a type? – Mark Kaplun Apr 25 '17 at 16:11
  • I'm not sure. I've updated my post with the code I used to print out the list of registered post types, and "user" isn't in the list. – rmmoul Apr 25 '17 at 16:20
  • IIRC the post type is being translated directly to the SQL query, so one thing to check for is to dump the actual query. Should probably also look at the DB and check if there are any post with such type. My guess will be that it is a plugin doing it based on some taxonomy. – Mark Kaplun Apr 25 '17 at 16:28
  • Oi, it was a plugin that the client added to site. – rmmoul Apr 25 '17 at 16:29
  • 1
    looking a little more, I see that the post author is 0, which is not possible, so my bet is that the "user" is being inserted by a plugin – Mark Kaplun Apr 25 '17 at 16:29
2

I tracked the issue down to a plugin, "Relevanssi Premium Search Options", that the client was using on their site and buried in the options was a setting for including users in the search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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