Improve Search Results in Textpattern
One contributing factor to Textpattern’s power as a content management system is its inherent templating flexibility. This clean slate is key when tweaking and optimizing search results. In fact, it’s so open, that customizing the search function is one of the leading questions within the forums.
Textpattern’s out-of-the-box functionality is basic, and the tags are not particularly robust. However, with some creative forms and some first-class plugins, the usability of your search results can take leaps forward in terms of usability.
Dedicate a Page and Section to Search
By default, Textpattern will re-use the default page to display search results. This is not always convenient. To separate these two things, create a new page and section dedicated to displaying search results.
First, go to Presentation > Pages and duplicate the default page, calling the new one “search.” Then go to Presentation > Sections, create a new section called “search,” and make sure to assign the page you just created as the template, and set all options to “no.”
Somewhere in your templates, you will have a <txp:search_input /> tag. To assign the section you just created, add the attribute section="search". (This only works when Textpattern is in clean URL mode. In messy mode, check out this older but still functional tutorial from Justin French.)
This allows for full customizion of the search page without mucking about the default page.
Show the Result Count
Textpattern provides the tag <txp:search_result_count /> that will display the number of returned hits. However, in order to function, it must be accompanied by a <txp:article /> tag, which actually counts the results. This is best illustrated through a code example:
<txp:if_search>
<txp:article pgonly="1" />
<txp:search_result_count />
<txp:article form="search_results" />
</txp:if_search />
The <txp:article /> tag with the attribute pgonly="1" performs the actual count; the following tag is used to display the count. The <txp:search_result_count /> must come second.
Restate the Search Term
One great way to improve the usability of search results is to restate the search string at the top of the results page. This not only reminds them of their query, but will help explain if no results appeared because the search term was misspelled.
Textpattern does not supply this functionality natively; instead, we have to rely on the plugin tru_tags. Besides being an amazing tagging plugin by itself (this site uses it), it provides a sweet little bonus tag <txp:tru_tags_search_parameter /> that returns whatever the user typed into the search box. For example, using the result count above, you could run both of these techniques in tandem:
<p>There were <txp:article pgonly="1" /><txp:search_result_count text="results" /> for the search "<txp:tru_tags_search_parameter />."</p>
This would return something like There were 8 results for the search “your mom.” It really is as simple as that.
Restrict Search Results to a Section or Category
I recently had a project that required the capability to search within only a few categories and a particular section, in addition to a primary site-wide search. The ability to search just one section is fairly well documented. However, the ability to search within particular categories requires a bit more legwork.
The plugin chh_article_custom serves as a replacement to the <txp:article_custom /> and <txp:article /> tags. In addition to some general bells and whistles, it has a few unique bits that include an expansion on the searchall attribute. When the variable is set to searchall="none", search results are restricted to the attributes of the tag. For instance:
<txp:chh_article form="search_results" limit="999" searchall="none" section="bigcats" category="Tigers,Lions,Panthers" />
This will restrict search results to the section “bigcats” as well as the categories “Tigers,” “Lions” and “Panthers.” This is very useful for complex sites where sections and categories are used to isolate specific types of content.
What I Cannot Figure Out
All of this aside, the one major deficiency I find in Textpattern’s search is that the engine only goes through the titles and bodies of articles — I have yet to find a way to get TXP to also search excerpts, categories, custom fileds, posted dates and other pieces of data. There have been some very hackish solutions posted, but even after tromping around publish.php, the only thing I found was a reliable way to break all search functionality completely. If anyone has any non-programmer-friendly clues, let me know.
Comments.
Robin
- wrote the following on Thursday January 10, 2008
Kevin
- wrote the following on Friday January 11, 2008
Usamah
- wrote the following on Sunday January 20, 2008
Jamie Knight
- wrote the following on Wednesday January 23, 2008
Kevin
- wrote the following on Saturday January 26, 2008
Steve Lam
- wrote the following on Sunday February 3, 2008
Markus Merz
- wrote the following on Friday July 18, 2008
Dominik Lenk
- wrote the following on Wednesday January 21, 2009
