Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Search for certain pages only

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 997 Views
  • prawnstar
    Avatar
    Community Member
    116 Posts

    Search for certain pages only Link to this post

    I'm following the tutorial:4 tutorial for enabling search.

    Is it possible to limit searches to certain pages or even page types?

    I want to create a very simple "blog" style page for recipes, but only search the recipes "blog."

    Ideally, I'd like to set up other pages just like it, and have the search only search their respective blog items.

    Thanks!
    Chris.b

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Search for certain pages only Link to this post

    How about changing the Page.php function (with appropriate SOMETHING's

    function results($data, $form){
       $dosResults = $form->getResults();
       
       $dosNewResults = new DataObjectSet();
       foreach ($dosResults as $doResult)
       {
          if ($doResult->Title == "SOMETHING"
             || $doResult->URLSegment == "SOMETHING"
             || $doResult->Name == "SOMETHING")
          {
             //filtered
          }
          else
          {
             //keep
             $dosNewResults->push($doResult);
          }
       }

       $data = array(
          'Results' => $dosNewResults,
          'Query' => $form->getSearchQuery(),
          'Title' => 'Search Results'
       );
       $this->Query = $form->getSearchQuery();

       return $this->customise($data)->renderWith(array('Page_results', 'Page'));
    }

    Barry

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: Search for certain pages only Link to this post

    Thanks for that Barry! That's what I'm looking for, but I don't think I understand the URLSegment part well enough to make my query work properly.

    To test is I set up two pages that had "Salad" in their title. One page inside the /recipes section and one outside. With the query below I'm returning both, but I only want the one inside the /recipes section. I was able to get mixed results filtering by Title and Name, but URLSegment is definitely what I want to filter by.

    Actually...What would be preferable would be something like this:
    $doResult->URLSegment == "$TheCurrentSegment"

    where "TheCurrentSegment" is the section I'm searching in (ie. the recipes section).

    Any idea?

    // search
       function results($data, $form) {
          $dosResults = $form->getResults();
          $dosNewResults = new DataObjectSet();
          foreach ($dosResults as $doResult) {
             if ($doResult->URLSegment == "recipes")
             {
                // filtered
             } else {
                // unfiltered
                $dosNewResults->push($doResult);
             }
          }
          $data = array(
             'Results' => $dosNewResults,
             'Query' => $form->getSearchQuery(),
             'Title' => 'Search Results'
          );
          $this->Query = $form->getSearchQuery();
          return $this->customise($data)->renderWith(array('ArticleSearchResults', 'ArticleHolder'));
    }

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: Search for certain pages only Link to this post

    I just tried a slightly different approach using this tutorial. But I get the same results. Apparently I fail at URLSegment. Any suggestions are greatly appreciated.

    Using this function I should be removing the result found in mysite.com/otherpage/the_recipe, but it's not removing. I'm getting both results from the /recipes and /otherpage sections.

       function results($data, $form) {
          $data = array(
             'Results' => $form->getResults(),
             'Query' => $form->getSearchQuery(),
             'Title' => 'Search Results'
          );
          foreach($data["Results"] as $page) {
             if($page->URLSegment == "otherpage") {
                $data["Results"]->remove($page);
             }
          }
          $this->Query = $form->getSearchQuery();
          return $this->customise($data)->renderWith(array('ArticleSearchResults', 'ArticleHolder'));
       }

    EDIT: So I now realize that URLSegment is returning the actual recipe page URLSegment. So that's why it's not filtering the page. So what I want would look like this, except this obviously doesn't work.

    if($page->ParentURLSegment != ThisParentURLSegment)

    997 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.