Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Filter by multiple categories
Is it possible to wire up the /category URLs so I can filter by one or more categories?
e.g. I have two article category subtrees that define stock available at a bunch of locations:
- Location
- London
- Manchester
- Leeds
- Glasgow
- Product Type
- Variety A
- Variety B
- Variety C
- Variety D
My articles in the /products section use category1 to depict the product type and catgeory2 to specify the location that product type is in stock. Individual products are serial numbered so there won’t ever be two identical physical products at more than one location, but multiple locations could have a bunch of products of a particular variety.
I want visitors to be able to slice and dice this data:
<if::category parent="location">
e.g. /category/manchester
Show a list of all product types at the specific location given in the URL, split by product type
</if::category>
<if::category parent="ProductType">
e.g. /category/variety-b
Show a list of all products of the type given in the URL, split by the locations that stock them
</if::category>
<if::category parent="ProductType" AND parent="Location">
e.g. /category/london/variety-c
Show a list of products of the type given in the URL at the location given in the URL
</if::category>
Basically, I’m trying to use the URL to show a stocklist. And people/search engines need to be able to see what’s available at each location (either the whole set of products or those of a particular type) or to see which warehouses stock products of a particular type.
I can tweak the stocklist shortcode to show anything I can grab from the URL but I’m trying to get Txp to do the heavy lifting with regards passing me the various bits of data I can filter by. It works fine if I supply one /category item (either a location or a product type) but if I try to list both (e.g. /category/leeds/variety-d) it obviously returns nothing, even with the Breadcrumb permlink scheme (which I know isn’t the right tool for the job as it would need to have the full path to the tree in the URL).
I can maybe use <page::url type="3">
to grab the value but I’m not sure how to plug the various values into the stocklist’s article_custom tag to filter by one or both parameters. It automatically does it if only one category name is given in the URL, but not both.
I’m doing this on the homepage template, inside <if::category>
tags.
Any neat ideas? I’m probably over-complicating it.
P.S. this is Txp 4.8.8.
Last edited by Bloke (2025-03-27 02:05:25)
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: Filter by multiple categories
I vaguely remember there being a method using the match
attribute… (see Example 8 in the txp:article docs). Might that help?
EDIT: See also this long convoluted thread that leads on to what Oleg has just posted…
TXP Builders – finely-crafted code, design and txp
Offline
Re: Filter by multiple categories
Bloke wrote #339374:
I can maybe use
<page::url type="3">
to grab the value but I’m not sure how to plug the various values into the stocklist’s article_custom tag to filter by one or both parameters.
For this, once the values are grabbed, you might try
<txp:article_custom category="leeds, variety-d" match="category1, category2" />
This should result in
Category1 IN('leeds', 'variety-d') AND Category2 IN('leeds', 'variety-d')
so Location and Product Type intersection should be empty.
Offline
Re: Filter by multiple categories
Ah yeah, if that works in 4.8.8, I’m laughing. Thought it did an OR on multiple cats not AND.
So the trick to detect this situation, is to work out which of the cats I’ve been given in the URL by inspecting the parent. Assign them each to a relevant variable (perhaps) and then build the category
and match
attribute values based on whether I have one or two variables set.
Hopefully there’s a cleaner way to build those with the comma separator using the power of break
or breakby
or breakform
, but a lot of this cool functionality is in 4.9.0 so I might be out of luck.
The layout of the page is currently slightly more involved than just a simple article_custom as I’m using a pair of txp:category_list tags to iterate over each variable independently to build an accordion list of all products inside all locations. Easier than figuring out txp:if_different for multiple values inside a single container. But for this special case I might just rewrite that and use a dedicated article_custom tag because it’s faster and easier.
Thank you.
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
Offline
Re: Filter by multiple categories
etc wrote #339382:
It does, by default, but
match="category1, category2"
switches toAND
mode.
Clever and logical. I like it.
CF would be easier to handle here, but they have (yet) no tree hierarchy.
Don’t tempt 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: Filter by multiple categories
Bloke wrote #339374:
I can maybe use
<page::url type="3">
to grab the value but I’m not sure how to plug the various values into the stocklist’s article_custom tag to filter by one or both parameters.
It is possible (not sure for 4.8.8) that
<txp:article_custom match="Category1=3, Category2=2" />
will give you exactly what you need on /trigger/leeds/variety-d
-type URLs. Someone should document it if it works :-)
Offline
Re: Filter by multiple categories
Ooh, I’ll give that a whirl. And yes, I’ll document it!
Nice syntax reminder, thanks.
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: Filter by multiple categories
etc wrote #339384:
It is possible (not sure for 4.8.8) that
<txp:article_custom match="Category1=3, Category2=2" />...
will give you exactly what you need
It is inded possible. Holy amazeballs. Thank you. So the only thing I need to do now is check if what’s in the URL is a location or a product or both and assign the appropriate positional value to them to plug into the article_custom tag. If the searches were always /location or /location/product then I could leave it just like that. The tricky thing is that the client may want to permit /product and/or /product/location URLs, which means I need to switch the 2 and 3 round in that case.
This is, however easy:
<if::category parent="location">
<txp:variable name="cat1arg" value="3" />
<txp:variable name="cat2arg" value="2" />
<txp:else />
<txp:variable name="cat1arg" value="2" />
<txp:variable name="cat2arg" value="3" />
</if::category>
That indeed does assign them:
<txp:article_custom match='Category1=<txp:variable name="cat1arg" />, Category2=<txp:variable name="cat2arg" />' limit="999" />
Woot!
EDIT: Hmm, spoke too soon. Doesn’t work if they’re both specified in the URL. Debugging….
EDIT 2: Ah, I think I see what’s happening. It’s seeing both categories in the URL and <if::category>
is not just considering the category in the first position, like I thought it would. So when it tests if it’s of type ‘location’ it’s checking the one at the end of the list in the URL. Or maybe checking if they’re both of type ‘location’. *ponders*
Last edited by Bloke (2025-03-27 18:00:55)
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: Filter by multiple categories
Well now I’m confused.
TESTING: <page::url type="2" />
<if::category name='<page::url type="2" />' parent="location">
LOCATION IS FIRST
<txp:else />
PRODUCT IS FIRST
</if::category>
On /category/leeds that outputs that it’s TESTING leeds
and that LOCATION is first.
On /category/variant-A that outputs that it’s TESTING variant-A
and that PRODUCT is first.
On /category/leeds/variant-A that outputs that it’s TESTING leeds
and that PRODUCT is first.
Huh?
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
Offline
Re: Filter by multiple categories
etc wrote #339389:
Might it be
category
instead ofname
?
Why yes, yes, it totally might. Thank you.
Does beg the question what the difference between the two attributes is in conceptual terms!
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