Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-11-07 01:47:57

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,790
Website GitHub

Search articles across category

There’s been talk in the past of section-specific searches and indeed this works perfectly if you specify searchall="0" in the article tag that governs search results. You can even get Txp to display content from a different section to the one you’re viewing by adding &s=some-section to the URL.

However, in one client site, I have a website section that contains FAQ articles but it’s not built up from articles in one section – it’s built from articles across a number of sections. They’re tied together by category. So there’s a parent category called FAQ (that has no articles assigned to it) and a bunch of sub-cats under that category which slice the FAQs up into the logical groups, for displaying in an accordion. Articles are assigned to one of the subcats and are dragged into the FAQ list via <txp:category_list>:

<txp:category_list parent="faq" sort="title asc" exclude="faq" break="">
   ... accordion paraphernalia here ...
   <txp:evaluate test="article_custom">
      <ul>
         <txp:article_custom limit="999" category='<txp:category />' match="Category1" sort="Posted asc" pageby offset>
            <li>Article Title, Excerpt, etc.</li>
         </txp:article_custom>
      </ul>
   </txp:evaluate>
</txp:category_list>

What I’d like to do is offer a search feature that finds and displays articles only from articles in one of the subcats beneath the ‘faq’ category.

Can it be done natively? If so, I can’t fathom it.

Am I looking at plugin territory? If so, which one? etc_search maybe? smd_query? etc_query?

Or do I need to somehow construct a custom plugin or some PHP/SQL that does the query based on URL params that somehow populates the search results so that <txp:search_results_excerpt> works properly.

Thoughts and ideas welcome.


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

#2 2022-11-07 10:08:29

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,412
Website

Re: Search articles across category

Not sure about natively, but etc_search certainly allows you to craft a SQL custom query, and you can then restrict your search result to certain sections (or exclude). By way of example, see this post with quite a complex combination of results and smd_tags.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2022-11-07 10:40:23

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,790
Website GitHub

Re: Search articles across category

Ooh, thanks for the link. Handy blueprint as I hack around with this. Figured I might have to plugin my way out of this, and what better way than with the swiss army knife of search!

It’s kind of unfortunate that if we supply &c=some-cat in the URL along with a q= that it triggers category list context and ignores the query param. I wonder if we can tinker the processing order here so that any URL request with a query string will take the search into account too? That might allow us to natively search by category, section, author, etc all from the URL line.

*ponders...*


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

#4 2022-11-07 12:23:33

etc
Developer
Registered: 2010-11-11
Posts: 4,701
Website GitHub

Re: Search articles across category

Bloke wrote #334088:

What I’d like to do is offer a search feature that finds and displays articles only from articles in one of the subcats beneath the ‘faq’ category.

You might want to try depth attribute:

<txp:article depth ... />

Accessing this tag via ?c=faq&q=whatever URL should restrict the search to faq subcategories. In theory, at least :-)

Bloke wrote #334090:

It’s kind of unfortunate that if we supply &c=some-cat in the URL along with a q= that it triggers category list context and ignores the query param.

Does it? The search query is well taken into consideration in my tests.

Offline

#5 2022-11-07 14:10:09

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,790
Website GitHub

Re: Search articles across category

etc wrote #334092:

You might want to try depth attribute

Holy hand grenades. That’s awesome. Works perfectly. Thank you so much.

<txp:article depth ... />...

The search query is well taken into consideration in my tests.

Huh. I swear last night it was dumping me into category list view whenever I added c=somecat&s=somesection to the URL but today it’s behaving. My bad. Late night eyes…


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

#6 2022-11-07 14:23:23

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,790
Website GitHub

Re: Search articles across category

Solution:

<form role="search" method="get" action="<txp:site_url />faq/">
   <p class="search_input">
      <input type="hidden" name="c" value="faq" />
      <input type="search" name="q" placeholder="Search FAQs" size="25" value="" />
      <input type="submit" value="Go" />
   </p>
</form>

With this in the page template:

<if::search>
   <txp:article depth listform="search_results" label='<if::category><txp:category title /> s<txp:else />S</if::category>earch results' labeltag="h2" />
<txp:else />
   // rest of page output
</if::search>

Genius. Thank you again.


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

Board footer

Powered by FluxBB