Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Site-wide search input and results in an own section
(This is using the current 4.9-dev version)
Suppose you decide to put your search form and results in an own “search” section, and have the following in your page template:
<!-- page top etc. -->
<txp:search_input form="search_input" />
<txp:if_search>
<txp:if_search_results>
<h3 class="page-subtitle">
<txp:items_count />
<txp:text item="matching_search_request" />
<q><txp:search_term /></q>
</h3>
<txp:else />
<txp:text item="no_search_matches" wraptag="h3" class="page-subtitle" />
</txp:if_search_results>
<txp:article[1] searchform="search_result_listitem" />
</txp:if_search>
<!-- page footer -->
and in the search_input
form I have set the form’s action
attribute to include the section so that the search form posts the results to the search section rather than the default section:
<form method="get" action="<txp:site_url /><txp:section />/">
<label for="search"><txp:text item="search" /></label>
<input id="search" name="q" type="search" placeholder="<txp:text item="search" />"<txp:if_search> value="<txp:search_term />"</txp:if_search>>
</form>
When I do this, I get the no_search_matches message. The debug trace shows the reason why: the search query looks for articles in the search section only – … AND Section IN ('search') …
– and as that has no articles of its own, I get no results:
[SQL: SELECT COUNT(*) FROM textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND Section IN ('search') AND Status IN (4) AND (`Title` LIKE '%searchterm%' OR `Body` LIKE '% searchterm%') ]
If I use the normal action attribute, the results are bounced back to the default page.
How can I use a dedicated section for the search input and the search results but stop the query restricting matches to just that section?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Site-wide search input and results in an own section
Don’t you add section="search"
to the search_input tag so it knows which section to divert to? Or is that only of use if you’re not using your own form? It’s been a while so I’m a bit rusty.
Last edited by Bloke (2024-03-04 15:25:49)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
Bloke wrote #336828:
Don’t you add
section="search"
to the search_input tag? It’s been a while so I’m a bit rusty.
You do if you’re getting the tag to generate the form but not if you’re using your own form.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Site-wide search input and results in an own section
Yes, I realised that after I posted. See edit.
I’m not entirely sure then. Seems like a bug to me.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
Does the URL contain the q = parameter?
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
Also, a long shot, when diverting searches to other pages, Do you have to mess around with searchall attribute and the include in site search checkboxes?
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
I’ve just checked on the textpattern demos, and am wondering if this is perhaps a 4.9 bug?
Do this on both the demo sites:
- duplicate the default page template and call the duplicate template ‘search’.
- create a ‘search’ (page template: search) and ‘test’ section (page template: articles)
- duplicate the demo article a few times, set to live and distribute across the test and articles sections
- add
<txp:section />/
to the search_input form’s action attribute
On v4.8.8: if I then visit the /search section and search say for “creativity” using the search input there – the search form produces a /search/?q=creativity
and I get matches back for all the sections – as I had expected. This is the query as one would expect:
[SQL: SELECT COUNT(*) FROM textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND Status IN (4) AND (`Title` LIKE '%creativity%' OR `Body` LIKE '%creativity%') ]
On 4.9.0 dev, I have to use messy urls (I get an nginx 404 otherwise*) and search for ‘content’ (due to different demo article text) but using the search form there to get /?s=search&q=content
returns Sorry, no results found matching your search request
. This is the query with an additional Section IN ('search')
:
[SQL: SELECT COUNT(*) FROM dev_textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND Section IN ('search') AND Status IN (4) AND (`Title` LIKE '%content%' OR `Body` LIKE '%content%') ]
Currently both demos are set up like that if you want to investigate, but I’m not sure for how long.
4.8.8: https://release-demo.textpattern.co/search/?q=creativity
4.9.0: https://dev-demo.textpattern.co/dev/?s=search&q=content
—-
*is the lack of clean urls on the 4.9.0 dev demo site a known issue, or is that something that needs fixing?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Site-wide search input and results in an own section
Right. Because by assigning s=search
to the URL, it’s assuming you’re searching the search
section. And thus not finding any articles assigned to it.
Seems as if the search part of pretext might need a tweak to fix this.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
Seems like it’s defaulting to ‘search current section’ in this case, rather than searching the sections as governed by ‘include in site search’.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Site-wide search input and results in an own section
Yes, that sounds logical. just different to how it was in v4.8.8. It’d be great to restore that to the not in (excluded sections) query we had before.
EDIT: The solution seems to be what you wrote above:
searchall="1"
can be used to show search results for all searchable sections.
Stupid me. Was beginning to wonder if I was losing it there 😅
TXP Builders – finely-crafted code, design and txp
Offline