Joomla 1.5, 1.7, 2.5: Customizing search results

Joomla includes component view overrides in templates. This is the ideal technique for restyling and rearranging your search results in Joomla. If you are not familiar with the output overrides, they are explained well in an old post on the Joomla Documentation site. That post discusses Joomla 1.5 explicitly, but the template overrides have changed very little (if at all) between 1.5 and 2.5.

Custom search results require two steps. The first step is to copy the file "default_results.php" from the search component to our template. By positioning it appropriately in the template directory, Joomla will use the template override instead of the component view. Using this technique assures us that a Joomla core update will never replace the updated file. To spell this out, if we make the update to the default_results.php file in the core search component, and Joomla releases an update that affects that very file, the update will overwrite our changes. Since Joomla does not update your custom template directories, you can be certain that your changes will never be overwritten by a Joomla core update. An administrator could still change templates, or remove your file, but that is a whole other issue.

Override the default_results in your template

Specifically, we need to copy the original /components/com_search/views/search/tmpl/default_results.php file to the active template directory /templates/myTemplate/html/com_search/search/default_results.php. For reference, I am using Joomla 1.7.3 (this also works in 2.5) with Phoca Gallery 3.1.5 and Phoca Gallery Search Plugin 3.0.1; the line numbers may be different in 2.5.

I am going to use this Gist at github for these updates. The original revision is the default_results.php file that comes with Joomla. That link is the final, and if you have search results that return with a $result->catid value (like some Phoca components do...), it will display a custom output for those results.

Edit the default_results in your template

In order to test that our template view override is working, try adding a simple header to the top of the results. Update line 13 of default_results.php to include some custom header text (like so). Now execute another search and notice your custom header immediately before the search results display.

Technically, we have a custom search results display. However, that is a pretty worthless customization. I want to make a couple of other updates. First, I want to address what I consider to be a bug. If you keep the Search - Categories plugin enabled, you will notice that category search results feature a nice bottom line that reads, "Created on" with no date. This may be user error, or configuration issues with access control; I have not investigated thoroughly. Either way, it is possible to get this behavior, and I think it is safer to check that a value is available before displaying a string in front of it. To that end, I want to add a couple checks to validate that the '$result->created' value is both set, and not equal to '' (line 36 here).

Running a new search with Category results should display a category result with no "Created on" text. And in fact, any search result without an available created on date, will avoid displaying said text.

Customize results output based on the type of result

Another update I want to make is to customize one type of search result. To do this, I first want to explain that you can achieve this in two different ways. The first technique is to change the plugin, which you achieve by knowing that the view file we are editing outputs specific values ($result->title, $result->section, $result->text, etc.); you simply change those values in the plugin. This is accomplished in a /plugins/search/COMPONENT/COMPONENT.php file - where 'COMPONENT' is something like 'content', 'newsfeeds', or some custom search plugin. A second technique is to add checks in the default_results.php file to determine what kind of search result you have, and disply it uniquely. Each of these techniques has merits depending on the situation. Generally speaking, I would encourage the first technique (changing the plugin.php file) if you only want to achieve changes for a particular type of search result, and if you want more global changes (like removing category, or placing category on the same line as the search result) you should make the change in the template.

For now, we are going to stick with the default_restults.php file, and make an update there. To find a good way to distinguish a type of search result, let us use a good old print_r in our results loop (like this). Now execute a search with multiple types of results (contacts, content, categories, etc.). It gets a little tricky here, you will need to determine an appropriate check for your content. It could be that your content always has the same $result->section value, that the $result->section value always starts/ends with some string, or that your 3rd party component result always has a value $result->someOtherValue - and when that is present you want to do something else.

No matter what your conditional check requires, the code update will be similar. Our code needs to add a conditional to the results loop, that outputs one bit of code for one condition, and another bit of code for all other conditions. I added a conditional check for a $result->catid value here, with a simple placeholder heading. The h2 value in the Gist is really just to confirm that your conditional is working properly. For your actual change, I would start by copying the original result code, and editing it to meet the needs of your update. The actual PHP and html edits are beyond the scope of what I wanted to achieve here, but some ideas would be changing the class for a particular type, so that you can alter the background color, or perhaps removing the section output for some results.

Related to this update, if you simply decide that you want to heavily restyle the search results, you can simply make edits to the default_results.php file in your template, and not worry about any conditionals. For reference, the final version of the custom search results Gist has the print_r removed, and displays only the title and a hardcoded string for search results that meet the condition.

For those of you who do not like to click on links, here is the final revision:

In my next article, we will update search results using the alternate technique, changing the PLUGIN.php file so that it provides alternative data to your view.


Joomla 1.5, 1.7, 2.5: Customizing search results — 30 Comments

  1. hello, not sure if my earlier message posted. Great article! Was able to remove the “created on” section. Thanks. Now I’m having issues getting virtuemart 2’s product images show in the search result, using the same layout as in category page. Help will be appreciated.

    • Each component will likely pass unique values to the search results, it’s likely a matter of debugging the values that are passed such that you can:

      a) Determine when a value is a virtuemart 2 product

      b) Alter the HTML output dependent upon the result of (a).

      The code above makes the (a) determination with “if (isset($result->catid))” and uses that conditional construct to appropriately affect the layout (b). You’ll probably need to replace the ‘$results->catid’ value with something specific to Virtuemart2. I don’t have any experience with that component, but I imagine they’ll be passing something unique enough to make the (a) determination.

  2. hello, great write up here, thanks! I was able to remove the unnecessary “created on” from search results.
    Is it also possible to allow the search results to display virtuemart thumb images in the same layout as in the Category page, with the items in rows of 3 or more?

  3. Hi, thank you for this great solution.
    I’m using a plugin “called simple image pro” which displays galleries using tags:


    I have lots of galleries with no or little text,
    Is it possible to make the plugin run in the search results and display the galleries?

    • It’s difficult to accomplish this at the plugin level. The search component reviews article/page content before plugin data is rendered, so given your example, the only text the search effort sees is “folder”.

      If the plugin is part of an extension suite that includes a component, where your galleries/images are actually stored, then you can set this up in exactly the same fashion as above, but it will likely require a unique isset check (like ‘catid’ above).

      Another difficulty is the little or no text issue – you would need at least image captions or titles (they wouldn’t necessarily need to display), otherwise the search component will never find search matches.

      • Hi imperialWicket,

        thank you for your reply.
        when I use the meta tag in the joomla article I can add as many words to search on…

        joomla3.testcarion dot be is an example of a page
        http://joomla3.testcarion dot be/index.php/component/search/?searchword=ral6000&searchphrase=all&Itemid=144 are the search results.

        as you can see the tags {gallery}…./gallery} are stripped out, only the foldername is displayed. e.g.: alu/Details

        the plugin is not part of a component. It makes a gallerie when you put a folder between {gallery}…./gallery} 🙂

        • I haven’t used Simple Image Gallery Pro, but given the additional info I would investigate:

          1. Using the K2 extensions. SIGPro states that it has clean K2 interfacing. Customizing search results for K2 content is straight-forward, as images and additional details are stored in a very well-structured way.
          2. Careful and cautious text parsing. If you nest all your sub-directories such that you can do a text search on the content and find “/siGalleryFiles/” (or something guaranteed to avoid false positives), then search results can parse for that value, and grab one of the images in the directory.

          K2 is an extremely useful extension, and I’d probably investigate that option first.

          Hope this helps!

  4. Hi Wicket and thank you for this great article!

    I have a question if you can help me about images in the search results. I’m runing Joomla 2.5 and i need the images and intro text (from com_content articles) to be displayed in the search results. I turned every forum uspide down and no answer for this issue 🙁
    I’ll be greatful if you could advise me how to alter the code to get this working.

    Best regards,

    • You’ll want to make edits to the /administrator/components/com_search/helpers/search.php file in the core search component. See the notes to Libby below, Joomla strips html tags in its summary posts, mostly for consistency in styling and a decent safety net.

      It’s definitely possible to edit that file and get what you want, but if you’re early in development, extensions like K2 might be a big help for a more media-friendly site.

      • Thanks man! You’re bringing the fastest support ever 🙂
        Yes i’m in early stage of developing the site and i checked K2 (not very familiar with ti), but i could not figure out is there an image in K2 default search or i need a different module/ plugin.
        I found a multiple extra fields filter module on K2’s site, but i was not sure if this would be the best choise (i will need filters also).
        If you have something in mind for K2 like module or something i’ll be happy to check it out.

        Thank you very much for your reply and your help!

        Best regards,

        • The resulting update with K2 installed would use the same technique as the original post, you determine if the search result is a piece of K2 content, and then you’re guaranteed that it has certain fields (like and image or a video), and you simply add the image/video/whatever output to your search result in the template file.

          I posted instructions for doing this with Phoca Gallery content, the only thing that would change is how the content is structured and the array keys.

          The benefit to using K2 content, is they provide a very structured way of supplementing content with media, and so everything becomes more predictable. Without K2, you end up doing a lot of questionable text parsing and handling odd edge cases to be sure that your search results don’t get fubar when a piece of content does not have an image (or has two, etc.).

          My schedule is pretty packed at the moment, but I’ll try to post a K2 search results post soon, I think it would benefit a lot of Joomla users.

  5. hi there , i need some advice. i manage to remove strips but i need to place some images, and im having big trouble with that… i wonder how gabe did to place youtube video..

  6. catid is specific to the Phoca extensions.  If you aren’t using Phoca extensions, it’s highly likely that none of your search results will ever have a catid value.

    Try using a print_r(); statement as shown here to figure out a good variable for distinguishing your result types.

  7. In the search.php file I mentioned, I think replacing the return statement should allow images to come through.

    The default is “return self::_smartSubstr(strip_tags($text), $searchword);”

    Change it so that there is no ‘strip_tags’ function on the return text: “return self::_smartSubstr($text, $searchword);”

    I’ll echo my earlier concern that CSS becomes important and tricky if you allow html to come through in your search results.  

  8. This has been helpful, however $result->catid doesn’t seem to exist. I’ve tried simply echoing it and nothing shows up. My goal is to simply display the article category with a link to that category. Need an ID to fill in the missing piece and without catid I can’t get anywhere. Any idea why catid wouldn’t exist or any other options that I could use to get this to work? Using 2.5 btw.

  9. Like Libby, I too need to show the introtext with html and images in the search results.  I am using joomla 2.5 for the site where I need the search modifications.  Can you point me in the right direction to find specifics on how to modify the necessary files.  Thanks!

  10. Hi
    Great article – many thanks. I am trying to show the intro text from articles in the search results as it is, without stripping out the html and images. Do you think that’s possible?

    • It’s definitely possible, but it can make styling your results a bit of a mess.  You’ll need to modify the core com_search helper.  The function that strips html and handles highlighting the search term for your results is in the /administrator/components/com_search/helpers/search.php file, and you’ll need to modify the ‘prepareSearchContent’ function.  

      One of the usual issues with this is that when you don’t strip html tags, your results usually end up with tags that are not closed.  For example, a paragraph tag starts, but isn’t closed until a point beyond what is included in the result text.  Paragraph tags don’t pose an issue, as most browsers will try to close them auto-magically, divs and spans and other containing tags are the ones that cause styling nightmares.

  11. Great post! I have a questions. I have a website where every article has a youtube video embedded. I want the search results to display article titles and the video from that page. Possible to do with the core search component or do I need to install an extension? Any suggestions on how to do this?

    • If you are manually embedding a youtube video in each article, it will take some search plugin and template updates.  
      You would need to parse the fulltext/introtext of the article in plugins/search/content/content.php and pull out the youtube link.  Store that value in the results array with an appropriate key (‘youtubeLink’, maybe?).  Then update your template’s default_results.php file (referenced in the post) so that if it finds the ‘youtubeLink’ key, it outputs it appropriately.  

      It’s quite similar to what we did in this article, but you have a little more text parsing to do.  This is very similar to what I have working for the next article, maybe I’ll put together a youtube or image specific search results plugin – that’s a good idea.