Download EE 2 Docs Download EE 1 Docs
ExpressionEngine

2.10.1 User Guide

Displaying Search Results

Search Results Tag

The Search Results Tag controls how you display results from your searches. Example:

<table border="0" cellpadding="6" cellspacing="1" width="100%">
    <tr>
        <th>{lang:title}</th>
        <th>{lang:excerpt}</th>
        <th>{lang:author}</th>
        <th>{lang:date}</th>
        <th>{lang:total_comments}</th>
        <th>{lang:recent_comments}</th>
    </tr>

{exp:search:search_results switch="resultRowOne|resultRowTwo"}

    <tr class="{switch}">
        <td width="30%" valign="top"><b><a href="{auto_path}">{title}</a></b></td>
        <td width="30%" valign="top">{excerpt}</td>
        <td width="10%" valign="top"><a href="{member_path='member/index'}">{author}</a></td>
        <td width="10%" valign="top">{entry_date format="%m/%d/%y"}</td>
        <td width="10%" valign="top">{comment_total}</td>
        <td width="10%" valign="top">{recent_comment_date format="%m/%d/%y"}</td>
    </tr>

    {if count == total_results}
        </table>
    {/if}

    {paginate}
        <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
    {/paginate}

{/exp:search:search_results}

</table>

The Search module uses the same pagination syntax as all first-party modules. Please look at the Pagination documentation for more information.

Parameters

backspace=

backspace="7"

Backspacing removes characters (including spaces and line breaks) from the last iteration of the loop. For example, if you put a <br /> tag after each result field you’ll have this:

Entry 1<br />
Entry 2<br />
Entry 3<br />

You might, however, not want the <br /> tag after the final item. Simply count the number of characters (including spaces and line breaks) you want to remove and add the backspace parameter to the tag. The <br /> tag has 6 characters plus a new line character, so you would do this:

{exp:search:search_results backspace="7"}
        {title}<br />
{/exp:search:search_results}

That will produce code like this:

Entry 1<br />
Entry 2<br />
Entry 3

Variables

Nearly all of the Channel Entries Tag Variables are available for the search results page so that you can display as much or as little of the channel entry’s data as possible depending on your needs. There are also some search results specific variables available in the results page:

auto_path

{auto_path}

This parameter is replaced with the URL to the entry with the URL Title appended to the end. Unlike other “path” variables, this variable does not require the Template_Group/Template to be specified. Instead, the path will automatically be determined by the Search Results URL setting for the channel in Channel Management.

excerpt

{excerpt}

An excerpt from the entry. The excerpt consists of the first 50 words from the field specified for search excerpting in your Channel Management settings for your channels. HTML markup is stripped prior to output.

full_text

{full_text}

The text from the entry. Unlike the {excerpt} variable, this one returns the entire text from the field specified for search excerpting in your Channel Management settings for your channels.

id_auto_path

{id_auto_path}

This parameter is replaced with the URL to the entry with the Entry ID appended to the end. Unlike other “path” variables, this variable does not require the Template_Group/Template to be specified. Instead, the path will automatically be determined by the Channel URL setting for the channel in Channel Management </cp/admin/channels/channel_management>.

member_path

{member_path='member/index'}

The Template_Group/Template with which to display the member profile of the author of the entry. Typically, this variable will be specified as {member_path=’member/index’}.

Search Keywords Tag

This tag lets you display the keywords used to perform a search. It is used on the search results page in order to show the user exactly what search terms they used:

{exp:search:keywords}

This may also be used on the template specified by the no_result_page parameter of the simple search form and advanced search form.

There are no parameters or variables associated with this ExpressionEngine tag.

Search Total Results Tag

This tag lets you display the total number of results found during a search. It is used on the search results page to show the total number of matches:

{exp:search:total_results}

This may also be used on the template specified by the no_result_page parameter of the simple search form and advanced search form.

There are no parameters or variables associated with this ExpressionEngine tag.

You may alternatively use the tag pair:

{exp:search:total_results}
        {total_results}
{/exp:search:total_results}

The only variable associated with this tag is {total_results}.

User Contributed Notes

Posted by: stinhambo on 6 May 2012 6:07am
stinhambo's avatar

You can also use the {paginate_links} variable pair for better control over the pagination styling -

http://ellislab.com/expressionengine/user-guide/modules/channel/pagination_page.html#pagination-links

Posted by: Derek Hogue on 18 April 2012 10:45am
Derek Hogue's avatar

If you want to track internal searches via Google Analytics, you’ll have trouble doing this via the search results URL, as the search is hashed. You can get around this by adding some conditional statements in your Google Analytcs tracking code, e.g.,

var _gaq _gaq || [];
  
_gaq.push(['_setAccount''UA-XXXXXX-X']);
  
_gaq.push(['_trackPageview']{if segment_1 == 'search' && segment_2 == 'results'}['/search/results/?q={exp:search:keywords}']{/if}); 

Your segment testing may differ based on your template setup of course.

Posted by: adamwiggall on 17 April 2012 4:24pm
adamwiggall's avatar

It is to be noted that the

{total_results} 
tag can be used within
{exp:search:search_results} 
tags without the use of a
{exp:search:total_results} 

tag pair.

e.g.

{exp:search:search_results}

  {if count 
== 1}
    
<h1>Search for: {exp:search:keywords} returned {total_results} result{if total_results 1}s{/if}.</h1>
  
{/if}

    
... result output goes here ...

{/exp:search:search_results} 
Posted by: Derek Hogue on 24 August 2010 12:43pm
Derek Hogue's avatar

Carry-over notes from the previous docs:

To make sure URLs from the Pages module are used in search results, use the following code:

<h3><a href="{if page_url}{page_url}{if:else}{auto_path}{/if}">{title}</a></h3

To display language indicating total number of results:

<p>You searched for <strong>{exp:search:keywords}</strong
and 
got {exp:search:total_results}{total_results}{/exp:search:total_results} result{if "{exp:search:total_results}" != 1}s{/if}.</p

You must have an EllisLab product license and have at least 50 posts to the community forums to contribute notes to the User Guide