Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Search result page: use MetaDescription as description (just like Google Search)

Go to End

10 Posts   3342 Views


15 January 2010 at 2:52am Community Member, 10 Posts

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 ( I found out there are some possilbities, but no MetaDescription in there.

Any ideas?


15 January 2010 at 5:06pm (Last edited: 15 January 2010 5:15pm), Community Member, 105 Posts

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() {
      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.


15 January 2010 at 9:14pm Community Member, 10 Posts

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


16 January 2010 at 2:02am (Last edited: 16 January 2010 2:08am), Community Member, 901 Posts

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:

<% if Results %>
   <ul id="SearchResults">
   <% control Results %>
         <a class="searchResultHeader" href="$Link">
         <% if MenuTitle %>
         <% else %>
         <% end_if %>
         <!-- instead of this: -->
         <!-- use this: -->
         <a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
   <% end_control %>
<% 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.


16 January 2010 at 2:10am Community Member, 10 Posts

Thanks Banal ;) Yes, this was my idea also I used this, but this gives a empty output.


16 January 2010 at 2:37am (Last edited: 16 January 2010 2:37am), Community Member, 901 Posts

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


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

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


16 January 2010 at 2:52am Community Member, 10 Posts

You are the man!


25 August 2010 at 11:22am Community Member, 121 Posts

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.



Go to Top