Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Wie man Suchergebnisse für bestimmte Beitragstypen in WordPress einschränkt

Hinweis der Redaktion: Wir erhalten eine Provision für Partnerlinks auf WPBeginner. Die Provisionen haben keinen Einfluss auf die Meinung oder Bewertung unserer Redakteure. Erfahre mehr über Redaktioneller Prozess.

Haben Sie sich jemals gefragt, wie Sie Ihre Suchergebnisse auf bestimmte Beitragstypen beschränken können? Das ist gar nicht so schwer. Wir haben Ihnen bereits gezeigt, wie Sie die Suchfunktion in WordPress deaktivieren können, indem Sie die Datei functions.php ändern. Jetzt werden wir das Gleiche tun, nur um unsere Suchergebnisse zu filtern.

Öffnen Sie Ihre functions.php-Datei und fügen Sie die folgenden Codes ein:

function searchfilter($query) {

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

return $query;
}

add_filter('pre_get_posts','searchfilter');

Beachten Sie die Zeile, in der steht

$query->set('post_type',array('post','page'));

Sie können die Suchergebnisse filtern, indem Sie die Werte in der Array-Variablen ändern. Im Moment ist es so eingestellt, dass Beiträge und Seiten angezeigt werden, aber Sie können es so ändern, dass alles angezeigt wird, was Sie wollen.

Offenlegung: Unsere Inhalte werden von unseren Lesern unterstützt. Das bedeutet, dass wir möglicherweise eine Provision verdienen, wenn Sie auf einige unserer Links klicken. Mehr dazu erfahren Sie unter Wie WPBeginner finanziert wird , warum das wichtig ist und wie Sie uns unterstützen können. Hier finden Sie unseren redaktionellen Prozess .

Das ultimative WordPress Toolkit

Erhalte KOSTENLOSEN Zugang zu unserem Toolkit - eine Sammlung von WordPress-bezogenen Produkten und Ressourcen, die jeder Profi haben sollte!

Reader Interactions

27 KommentareEine Antwort hinterlassen

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Anna says

    Thanks for this code – it worked, although you last updated in 2013! My theme uses also an Instant Search and I would like to limit the results there aswell. How could I do that?

  3. Sparsh Goyal says

    My present theme shows post with few starting lines for searched term/word. I want to customise it to show that paragraph having searched term/word in post excerpt. In other words, I want to show related text in post excerpt not starting paragraph in search results. Can anyone help me to do this….

  4. Steven says

    I’ve got an easy function in my themes functions.php file, which should only filter the Posts by a search term… when I search something now, the HTTP 500 Error „The website cannot display the page“ appears. Anyone got an Idea, whats wrong with my function?

    function searchFilter($query) {
    if ($query->is_search)
    {
    wp_reset_query();
    $args = array ( ’s‘ => $_GET[’s‘] );
    query_posts( $args );
    }
    }
    add_filter(‚pre_get_posts‘, ’searchFilter‘);

  5. Greg says

    This is restricting all search forms to the custom post type – including my sidebar searchform, which needs to return all results. This is working for me:

    function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
    if(isset($_GET[‚post_type‘])) {
    $type = $_GET[‚post_type‘];
    if($type == ‚book‘) {
    $query->set(‚post_type‘,array(‚book‘));
    }
    }
    }
    return $query;
    }
    add_filter(‚pre_get_posts‘,’searchfilter‘);

    • Jonathan Joosten says

      Thanks for the assist, I improved your code so people can only search allowed post_types.

      function searchfilter($query)
      {
      if ($query->is_search && !is_admin() )
      {
      if(isset($_GET[‘post_type’])) {
      $types = (array) $_GET[‘post_type’];
      $allowed_types = get_post_types(array(‘public’ => true, ‘exclude_from_search’ => false));
      foreach($types as $type)
      {
      if( in_array( $type, $allowed_types ) ) { $filter_type[] = $type; }
      }
      if(count($filter_type))
      {
      $query->set(‘post_type’,$filter_type);
      }
      }
      }
      }
      add_filter(‘pre_get_posts’,’searchfilter’);

      • Dan Sz. says

        How is this implemented? If I’m reading Greg’s comment, correctly we want a single form that is limited to a post type, while keeping default search intact for other areas of the site.

        I’m asking because a site i’m working on needs a searchable „Resource Library“, which I’d like to build out with compromising the normal search functionality.

  6. Sandeep says

    Hi,

    I’m not able to restrict pages in search filter.
    I just need the search within the posts and not pages.

    $query->set(‚post_type‘,array(‚post‘));
    This displays pages too.

  7. Nick says

    I used this code to restrict my search results to Pages, not Posts. It worked in that regard, however, it seems to only search the Page names as opposed to content. For instance, if a user searches „pricing“, the Pricing page will be a result but if they search „price“ or „cost“ (both of which are words on the pricing page) nothing is found. Is there a snippet of code I’m missing?

  8. emre says

    Hi,
    I have a question and I couldn’t get a solution since last month.
    I have lost of categories, pages and re-directions in my blog so my search box finds many unnecessary results when you try to search something. I want to customize my search.php for only categories part. In other words, we you search something, the results should be only from categories sections. So I will be get rid of redundant & duplicated results. My current codes are as below…Please help me :)

  9. Felix says

    There’s one problem with your snippet:

    It limits the search results in the backend, you should wrap it with:


    if ( !is_admin() ) {
    // snippet
    }

  10. Alan Hughes says

    So how would you apply this to a specific search bar? It doesn’t just apply the filter to every search bar on your site would it?

Eine Antwort hinterlassen

Danke, dass du einen Kommentar hinterlassen möchtest. Bitte beachte, dass alle Kommentare nach unseren kommentarpolitik moderiert werden und deine E-Mail-Adresse NICHT veröffentlicht wird. Bitte verwende KEINE Schlüsselwörter im Namensfeld. Lass uns ein persönliches und sinnvolles Gespräch führen.