<p><strong>Edit: solved in comments</strong></p>
<p>My entries are set up as a parent &gt; 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.</p>
<p>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 &gt; child structure. If a child category is assigned to an entry, its parent category also applies to that entry.</p>
<p>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.</p>
<p>Example structure:</p>
<ul>
<li>[state: enabled] entry 1, category: A</li>
<li>[state: enabled] entry 2, category: none
<ul>
<li>[state: disabled] entry 2.1, category: B</li>
<li>[state: disabled] entry 2.2, category: C</li>
</ul>
</li>
<li>[state: enabled] entry 3, category: A</li>
<li>[state: enabled] entry 4, category: D</li>
<li>[state: disabled] entry 5, category: none
<ul>
<li>[state: disabled] entry 5.1, category: E</li>
<li>[state: disabled] entry 5.2, category: F</li>
</ul>
</li>
</ul>
<p>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.</p>
<p>So far I have this query but it is not considering the child entries' categories.</p>
<pre><code>{% set entries = craft.entries.section('mysection').limit(null) %}
{% set categories = craft.categories.group('mycategories').orderBy(&quot;title asc&quot;).relatedTo(entries).find() %}

{% for category in categories %}
    {{ category.title }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2023-07-17T10:10:14.360Z
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.
James Smith
CC BY-SA 4.0
2023-07-17T12:48:43.333Z
@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
CC BY-SA 4.0
2023-07-17T13:12:08.893Z
`{% if entry.level != 1 %}{% redirect entry.parent().url() %}{% endif %}`
James Smith
CC BY-SA 4.0
2023-07-17T13:15:07.827Z
Thanks, it really was that obvious and easy :)
Jake
CC BY-SA 4.0
2023-07-21T02:18:37.173Z
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
<craft3><templating><entries><categories>
How to list categories only related to enabled entries or disabled child entries
# How to list categories only related to enabled entries or disabled child entries

(Asked by: username_0 on 2023-07-17)

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 %}

**Tags:** craft3, templating, entries, categories
> **Comments:**
> **username_1**: 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.  
> **username_0**: @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?  
> **username_1**: `{% if entry.level != 1 %}{% redirect entry.parent().url() %}{% endif %}`  
> **username_0**: Thanks, it really was that obvious and easy :)  
> **username_2**: Either one of you want to add the solution as an official answer in case it ends up helping someone in the future? :)  

---

## 0 answers

