Edit: solved in comments

My entries are set up as a parent > child structure. A single entry can either have children or not have any. If parent has children, the children entries are put in a disabled state, because those children should not show up as individual entries on the frontend, but are instead shown as subitems on a parent's entry page.

Then, a category is assigned to a level 1 entry if it has no children; and in case of level 2 children, the category is assigned to each child but none to its parent entry. Categories themselves are also set up in a parent > child structure. If a child category is assigned to an entry, its parent category also applies to that entry.

I'd like to output a categories list on the frontend which lists all categories assigned to any level of entry, as long as its parent entry has an enabled state in backend.

Example structure:

  • [state: enabled] entry 1, category: A
  • [state: enabled] entry 2, category: none
    • [state: disabled] entry 2.1, category: B
    • [state: disabled] entry 2.2, category: C
  • [state: enabled] entry 3, category: A
  • [state: enabled] entry 4, category: D
  • [state: disabled] entry 5, category: none
    • [state: disabled] entry 5.1, category: E
    • [state: disabled] entry 5.2, category: F

I'm looking for the exact query to output A B C D because its parent entries are enabled, but not output E F because its parent entry is disabled.

So far I have this query but it is not considering the child entries' categories.

{% set entries = craft.entries.section('mysection').limit(null) %}
{% set categories = craft.categories.group('mycategories').orderBy("title asc").relatedTo(entries).find() %}

{% for category in categories %}
    {{ category.title }}
{% endfor %}
  • 2
    This does not answer your question, but if it's not too late I'd recommend you reconsider your approach here - the enabled/disabled state of an entry should not be abused in this way - you'll find yourself fighting against the system at every step. If the child entries should not be URL-addressable, redirect to the parent entry's URL in the template if entry.level is > 1. Don't disable them. Commented Jul 17, 2023 at 10:10
  • @JamesSmith Thank you, I had not considered this approach and tbh, I'm not even sure why I set it up the way I did. A quick test proves enabling the child entries likely fixes my issue. Now I'm struggling with the redirect code. How would I redirect a child's url to its parent url?
    – Jake
    Commented Jul 17, 2023 at 12:48
  • 2
    {% if entry.level != 1 %}{% redirect entry.parent().url() %}{% endif %} Commented Jul 17, 2023 at 13:12
  • Thanks, it really was that obvious and easy :)
    – Jake
    Commented Jul 17, 2023 at 13:15
  • 1
    Either one of you want to add the solution as an official answer in case it ends up helping someone in the future? :)
    – Brad Bell
    Commented Jul 21, 2023 at 2:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.