$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Change Search display for Custom Post Type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Change Search display for Custom Post Type

matteradmin9PV0评论

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');

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');
Share Improve this question asked Oct 24, 2018 at 8:57 SengelYTPISengelYTPI 151 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far