0

I want the search to not search for custom post type "mitgliederbereich" when on other pages, and when you are on the "mitgliederbereich" page, that only "mitgliederbereich" posts should appear in search. How can I achieve this? Here is my functions.php code for the function.

function mitgliederbereich_filter_search($query) {

   global $post;

   if ($post->post_type == "mitgliederbereich") {

    if (!$query->is_admin && $query->is_search) {
        $query->set('post_type', array('post'));
    }
    return $query;

   } else {

      if (!$query->is_admin && $query->is_search) {
        $query->set('post_type', array('mitgliederbereich'));
    }
    return $query;

   }

}
add_filter('pre_get_posts', 'mitgliederbereich_filter_search');
0

Try below code

function mitgliederbereich_filter_search($query) {
  global $post;
  global $wp_post_types;
  if ($post->post_type == "mitgliederbereich") {
      $wp_post_types['mitgliederbereich']->exclude_from_search = false;
  } else {
      $wp_post_types['mitgliederbereich']->exclude_from_search = true;
  }
}
add_filter('pre_get_posts', 'mitgliederbereich_filter_search');

Hope this helps.

  • Glad to hear that it helped you :) – Krishna Joshi Oct 24 '18 at 9:28

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.