Search Function

  • I am working on the website which uses the search function. There are a couple of issues relating to the search function. The site is running Perch Runway 3.2 and the server has PHP 7.3.25.

    On entering a search term, e.g. satin, the first page of search results are displayed. The problem is that the hyperlink to the next page (at the bottom of that page) conveys only the page parameter – there is no q (search query) or go (whatever that does), i.e. the link is

    How can I get it to generate the correct hyperlink, with search query?

    The second question is is it possible to search multiple directories? The options array of perch_content_search() allows searching of only part of the site, by defining from-path. Does this allow multiple paths or just one?

    The site was designed by someone else and originally had all the products in the /products/ directory. In my quest to shorten the URLs, removing the /products/ bit, I have moved some of the categories to the root. I have now realised that the search function isn't picking them up and that this is because from-path is set to '/products/' in search.php.

    I don't want the search function picking up the contact page, about page and so on, just the products. Is it possible to search multiple directories? Or might this explain why the products were placed in their own directory?

    What order does the search function output results? Is it simply in order that they were added to the site (i.e. by ascending page ID number)?

  • Since writing the above, I've discovered /perch/templates/search/search-result.html. In it we have the following which generates the 'Page # of #' and the 'Previous' and/or 'Next' links where applicable:

    I was reading the Perch documentation page on Search Tags which says that search_key is the value that was searched for. I amended the hyperlink on line 9 of the above as follows (modified bit in red):

    <a href="<perch:search id="next_url" encode="false" />&q=<perch:search id="search_key" encode="false" />">Next</a>

    This works. But this template, as shipped with Perch, doesn't include the bit I've added, which suggests it's not necessary and should otherwise work.

    What am I missing?

  • Gareth S

    Approved the thread.
  • Clive Walker is right here.

    The paging shown in the example template is just the standard pagination example, it makes no assumptions as to your URL structure. You could quite easily enough have some URL rewriting so that your search URL was, in which case the template example would work fine.

    You can probably tidy up the template code using the rewrite attribute on the paging tags. The docs for it are here

  • Thanks for your comments.

    The rewrite looks good for tidying up the URL. I came up with the following:

    <a href="<perch:search id="next_url" rewrite="/search/{page:%s/}" encode="false" /><perch:search id="search_key" encode="false" />">Next</a>

    This produces /search/2/satin where, in this example, '2' is the page number and 'satin' is the search. Obviously, I would want it the other way around, with search term followed by page number.

    The problem here, from what I can see, is that the rewrite can only be affected on the pagination query string because that alone is what next_url returns.