Using search in Textpattern RC3

Since revision 342, Textpattern handles searching in a different way. A few people seem to be having problems with this, so here’s a quick howto.

In order for searches to work, there has to be a <txp:article /> tag on the page that’s used to display the results. By default the search input form will use the front page to display searches. If you don’t have a <txp:article /> tag on your front page, the best way to handle searches is to use a separate section to display the results:

1. Create a new page template, named search_page. You can use your standard layout by copying an existing template, but make sure that it includes a <txp:article /> tag. This is the page that will be used to display the results.

2. Create a new section named search (or some other name, if you’d prefer). Uses page should be set to search_page. Selected by default, On front page, Syndicate and Include in site search should all be set to no.

3. On all other page templates where you include a search input form, change the code to point to your new section:

<txp:search_input section="search" />

The rev 342 updates allow complete control over the display of the search results. You can use the following tags in your search_results form:


<txp:search_result_title />
<txp:search_result_excerpt />
<txp:search_result_url />
<txp:search_result_date />

..plus all the usual article tags like <txp:excerpt />, <txp:posted /> and so on. And on your search_page template:

<txp:search_result_count />

One thing to be aware of: <txp:search_result_count /> won’t work if it’s placed before the <txp:article /> tag on the page template. That’s because the search is performed by the <txp:article /> tag, so the number of results aren’t known before then. Fortunately, there is a way to overcome this. The pgonly attribute tells the article tag to do all the counts for pagination and search, but not display anything. By using the article tag twice, once with pgonly and once without, we can display the search result count at the top of the list:


<txp:article limit=25 pgonly=1 />
<txp:search_result_count />
<txp:article limit=25 />

The same principle isn’t limited to search results, and applies to pagination tags also. It’s important that both article tags have the same attributes (other than pgonly), otherwise the counts won’t match the list displayed by the second tag.

If you’re displaying search results on your main page, experiment with the <txp:if_search> </txp:if_search> tag. You might also find the <txp:article searchform="mysearchform" /> attribute helpful.

One benefit of the new system is that any pagination tags that work with <txp:article />, such as <txp:older> and <txp:newer>, can be used to page through search results. Set the number of results to display using the <txp:article limit=n /> attribute on your search_page template.

22 May 2005, 05:00 by Alex ·

Great tips, Alex. Thanks a lot.

One thing I noticed: txp:search_result_count displays “articles found” without any number before it.

And: does the section have to be called “search” or could I give it its Germany equivalent name?

andreas    May 22, 07:33 pm    #

Cool tips, indeed. As Andreas noted, txp:search_result_count doesn’t display any number (for my query, there should be 10 articles).

While playing a bit with this, I tried pagination. txp:article limit="5". The links ‘work’ (prev, next), but a second page returns nil and zero, whereas it should displays 3 articles. But maybe that is because I use a definition list to wrap the search results. Need to play some more.

Philippe    May 22, 11:40 pm    #

search_result_count does work, but there’s a trick required to get it to display properly before the search results. I’ve updated the article to reflect this.

There’s nothing special about the ‘search’ section – the magic is all done by the @@ tag, so you can use any section name you like.

Alex    May 23, 02:04 am    #

Andreas, this forum thread”:http://forum.textpattern.com/viewtopic.php?pid=54828#p54828 discusses the answer to your question. Use this syntax:

‹txp:article pgonly=1 /›
‹txp:search_result_count /›
‹txp:article /›

andrew    May 23, 02:13 am    #

Ok, thanks. Got it working now. This little gem of an attribute pgonly=”1” will come in handy at other times, too. Great to know about it!

andreas    May 24, 11:37 pm    #

Alex,

Is it possible to limit the number of articles searched with a “Previous Page” “Next Page” link at the bottom. Similar to what we have with a standard articles page.

btw… I’m using a separate search page from default.

— Ray    May 25, 06:44 am    #

I just updated from r342 to r383, and in the change search results went from honoring the override form to ignoring it – is there something I can do to have the search results displayed with the override form? It is an override form after all..

Kjell    May 25, 07:55 am    #

Another search related how-to:
to display in search-results the searchterm use Tag txp:q .

— Ingo    Jun 13, 06:41 pm    #

Alex, pagination tags on the search results page lead to site.com/search&pg=2 instead of site.com/search?q=searchterm&pg=2, thus giving an empty page. I tried both txp:older/txp:newer and rsx_page_number_list, same output.

Els    Jun 28, 07:43 am    #

This is great. You should do a write up for textbook.

matt mikulla    Aug 6, 10:51 pm    #

Someone pointed your article out to me in the TextPattern forums and I’m grateful for that.

I would like to make the comment, though, that where you say:

“3. On all other page templates where you include a search input form…”

You should remove the word “other”. I read this and it seemed that the search form I had on my new search_page was exempt from this tag syntax. I beat my head on the table for a few hours until I decided to try that on the form and it worked!

Thanks for the write up! I appreciate it. :)

Yazmin    Aug 10, 01:35 am    #

Is there any way ,how I could limit the amount of words in my searchoutput form to a specific value ?

— marios    Mar 2, 08:17 am    #

Hi!
I am trying to configure my search result page.
I cannot get the search results to display as a list. Even if I know that there are seven articles containing my search word only one is shown. What should I do? Thank you = )

Jeannette    Aug 18, 09:05 pm    #

Commenting is closed for this article.