How To Display Custom Attributes In Search Results

Permalink 1 user found helpful
My custom attributes can be displayed in the page-list but not in the search results list.

I have many pages with common "page_thumbnails" and also many pages with a custom attribute "coverlink" (which is a complete link to a extern pic).

That works fine in page-list view.php but I am not able to get the coverlink-attribute value into my search results view.php to display the external pic instead of a "page_thumbnail" in the result list.

Is there a way to use custom attributes in search results?

MathiasB
View Replies: View Best Answer
bbeng89 replied on at Permalink Best Answer Reply
bbeng89
I'm not positive if this is the best way, but you should be able to do this:

$page = Page::getByID($r->getID());
echo $page->getAttribute('your_attribute_handle');


It looks like the results you are looping through in foreach($results as $r) are not actually page objects but are IndexedSearchResults objects. You can get the page id for an IndexedSearchResult by calling getID() and then use this to pull the actual page object.

I hope that helps!
MathiasB replied on at Permalink Reply
MathiasB
Hey bbeng,

Works fine, looks great.

Thank You Very Much for your quick help.
jolly replied on at Permalink Reply
jolly
Hi
I think this is what I am looking for.. I want to search for an attribute called part_number I set up in ecommerce and it is searchable in product list but I need it being found in a normal search block.. how and where did you add the suggested code?
MathiasB replied on at Permalink Reply
MathiasB
Dear Jolly,
I am very sorry, that I answer that late.
I must confess, that I am not very skillful in php (try and error freak), so I think it is the best, I post the complete code of my result page, where I inserted the code sniplets.
It is a template for a search block, which only displays results.

<?php   defined('C5_EXECUTE') or die(_("Access Denied.")); 
?> 
<?php   if (isset($error)) { ?>
   <?php  echo $error?>
<br/>
<br/>
<?php   } ?>
<form id="searchQuery" action="<?php  echo $this->url( $resultTargetURL )?>" method="get">
  <?php  
$tt = Loader::helper('text');
if ($do_search) {
   if(count($results)==0){ ?>
  <?php echo t('There were no results found. Please try another keyword or phrase.')?>
  <?php  }else{ ?>
  <div id="searchResults">

Hope it will help.

Best
Mathias