I have a list of entries in one of my channels that I need to filter by categories. I’ve read the docs quite a few times, looked online everywhere for similar problem but found nothing, tried every combination of it pretty much I can think of with no result. This is at least the 40th version of the template that I tried to filter (meaning ive tried with or without dynamic, with or without category=”“, with or without :categories, all of these combined in every possible way), tried it with all the combinations of the url with …/template/category/C5 or …/template/category/category-name (in this case changed the category to show name instead of id), tried …/C5 or …/category-name with no result.
All of the above cases shows all of the entries without filtering anything. What was working is if tried …/template/5 showed only id no.5 entry. That works, but not the category.
I have no idea what is the right combination to make the filtering work with URLs. Here is my latest version:
{exp:channel:entries:categories channel="review" orderby="order_on_page" sort="asc" limit="12" category_group="2" category="" dynamic_parameters="category[]" }
{some_variable}
{/exp:channel:entries:categories}
If anyone can help me out here to shed a light on what I’m doing wrong would be greatly appreciated.
One way to approach this would be to use the URL segments to drive your channel:entry parameters, and so obtain the filtered results you are after.
Suppose your URL is something like
https://domain.com/channel/category_id
You can translate this into a channel entries call thusly:
{exp:channel:entries channel="{segment_1}" category="{segment_2}" dynamic="no"}
... stuff ...
{/exp:channel:entries}
The dynamic="no"
is important - as this prevents EE from trying to guess your intent from the URL.
In your sample code:
order_on_page
is a legitimate option for the orderby= parameter… $_POST
or $_GET
values - either from a form ($_POST
) or by adding a query string to the URL ($_GET
). HTH 🐾
Thanks JCOGS, I’m going to try what you wrote.
In the meantime I’ve tried to troubleshoot the whole thing by stripping it back as much as possible and finally found out that giving it category_group=”2” was the one thing that actually broke the filtering. So currently the filtering works, but I have to try what happens when I have multiple category groups applied, might break the whole thing again.
ps. “orderby” is the only thing that can actually have a variable as a value, that works fine actually
I’ve made it work with my version, but its not as smooth of an experience as I’ve imagined it would be (xmlhttprequest on page load doesn’t block the page loading but also waits for everything else to load before actually doing something, and if I have a long page - which i do - it waits for a second or so before doing anything - and in the meantime i have a half blank page to stare at on page load because of that)
so I’m thinking about implementing your solution, but I have one thing that makes it out of reach for me - I don’t have url segments on the actual pages where the list appears. For each “main” page I use the “pages” add-on that generates nice urls - that is pretty much a must for SEO for clients.
Do you think its possible to add a category based on a field instead of segment - without xhr (or classic form as that reloads the page)?
{exp:channel:entries channel="review" category="{my_category_field}" dynamic="no"}
... stuff ...
{/exp:channel:entries}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.