Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Search result page: use MetaDescription as description (just like Google Search)

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

Page: 1 2
Go to End
Author Topic: 3091 Views
  • karlevid
    Avatar
    Community Member
    10 Posts

    Search result page: use MetaDescription as description (just like Google Search) Link to this post

    For my search result page I would like to use the MetaDescription (just like Google Search) instead of example $Content.LimitWordCountXML, this is used in the tutorial (http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search). I found out there are some possilbities, http://doc.silverstripe.org/doku.php?id=text but no MetaDescription in there.

    Any ideas?

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Search result page: use MetaDescription as description (just like Google Search) Link to this post

    SilverStripes out of the box search is pretty basic and only searches the Content and page Titles etc..

    What we do is create a normal form in the template and point its action at a new Page Types action eg:

    class SearchPage extends Page {

    ...

    }

    class SearchPage_Controller extends Page_Controller {

    public $searchEntry = "";
       public $resultsSize = 0;
       
       public function init() {
          parent::init();
          
          if(isset($_GET["browseall"]))
          {
             unset($_SESSION["search_input"]);
          }
          
          
          if(isset($_POST["search_input"]) && $_POST["search_input"] != 'Search Products') {
             $this->searchEntry = filter_var($_POST["search_input"], FILTER_SANITIZE_STRING);
             $_SESSION["search_input"] = $this->searchEntry;
          }
          else if (isset($_SESSION["search_input"]))
          {
             $this->searchEntry = $_SESSION["search_input"];
          }
          
       }

    public function doSearch()
    {
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1)
             {
                $_GET['start'] = 0;
             }
             $SQL_start = (int)$_GET['start'];

             $results = DataObject::get('Product',
                 "Title LIKE '%".$this->searchEntry."%' ".
                "OR Brand LIKE '%".$this->searchEntry."%' ".
                "OR PartNumber LIKE '%".$this->searchEntry."%' ".
                "OR MetaTitle LIKE '%".$this->searchEntry."%' ".
                "OR MetaDescription LIKE '%".$this->searchEntry."%' ".
                "OR FIND_IN_SET('".trim($this->searchEntry)."', `MetaKeywords`) > 0", "Title ASC", NULL, "{$SQL_start},".Page::$resultsPerPage);
                
             
           $title = empty($this->searchEntry)?'Search Results':'Search results for: '.$this->searchEntry;
             
             if ($results)
             {
                $this->resultsSize = $results->Count();
             }
             
             $data = array(
              'Results' => $results,
              'Query' => !isset($_GET['q'])?$this->searchEntry:$_GET['q'],
              'Title' => $title
           );

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

    }

    What we are doing here is calling '<searchpage-urlsegment>/doSearch' and searching the products but also MetaDescription and MetaKeywords you could modify this to search Page Content, Title instead fairly easily.

  • karlevid
    Avatar
    Community Member
    10 Posts

    Re: Search result page: use MetaDescription as description (just like Google Search) Link to this post

    Thanks, this seems to be the code I was searching for.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Search result page: use MetaDescription as description (just like Google Search) Link to this post

    Do you want to search the MetaDescription, or just output the MetaDescription in the Search-Results (instead of the Summary)?
    If the latter is the case, just use $MetaDescription instead of content?
    Here's an example with modified code from: http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search#showing_the_results

    <% if Results %>
       <ul id="SearchResults">
       <% control Results %>
          <li>
             <a class="searchResultHeader" href="$Link">
             <% if MenuTitle %>
                $MenuTitle
             <% else %>
                $Title
             <% end_if %>
             </a>
             <!-- instead of this: -->
             <p>$Content.LimitWordCountXML</p>
             <!-- use this: -->
             <p>$MetaDescription</p>
             <a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
          </li>
       <% end_control %>
    </ul>
    <% else %>
       <p>Sorry, your search query did not return any results.</p>
    <% end_if %>

    If you want to search the MetaDescription, you'll have to implement a search like the one CodeGuerilla provided.

  • karlevid
    Avatar
    Community Member
    10 Posts
  • banal
    Avatar
    Community Member
    901 Posts

    Re: Search result page: use MetaDescription as description (just like Google Search) Link to this post

    Ah yes. I see. The SearchForm class doesn't return the MetaDescription in the Results.
    I created a subclass of the SearchForm that also returns MetaDescription (kinda sucks that I had to copy the whole method for this tiny change..)

    1) Place the code from here: http://pastie.org/779513 in mysite/code/search/CustomSearch.php
    2) Run dev/build
    3) In your Page Controller change this line:

    return new SearchForm($this, "SearchForm", $fields, $actions);

    to

    return new CustomSearch($this, "SearchForm", $fields, $actions);

    You should then be able to access $MetaDescription in your Search Results..

  • karlevid
    Avatar
    Community Member
    10 Posts
  • vancouverWill
    Avatar
    Community Member
    121 Posts

    Re: Search result page: use MetaDescription as description (just like Google Search) Link to this post

    Have any of you tried updating the SearchEngine function similar to what Banal did but changing it so that it looks at more tables than just site tree and file. I have tried editing but haven't been able to fix the sql bugs I'm getting. Been trying on and off all week, coming from different angles and as yet no success.

    Cheers

    Will

    3091 Views
Page: 1 2
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.