<p>So I finally got there in the end and am posting in case it helps anyone else.</p>

<p>I was building a drop down menu with bootstrap that showed a 2 tier category / entry menu.</p>

<p>Code as follows:</p>

<pre><code>&lt;ul class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;

{% set catsL1 = craft.categories().group('products').level(1).all() %}    
{% for cat in catsL1 %} 

    {% set childCats = craft.categories.descendantOf(cat) %}
    {% set catParent = craft.categories.id(cat).one() %}
    {% set dontInclude = [] %}
    {% for child in craft.categories.descendantOf(cat).all() %}
        {% set dontInclude = dontInclude|merge(craft.entries.section('products').relatedTo(child).ids) %}
    {% endfor %}
    {% set str = dontInclude|join(', not ') %}
    {% set hideEntries = craft.entries({ relatedTo: cat,id: 'and, not '~str }) %}

        {% if hideEntries|length %}

        &lt;!-- if we have some entries in the parent category, output them in a dropdown menu --&gt;
        &lt;li class="dropdown-submenu dropdown"&gt;
            &lt;a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;{{ cat.title }}&lt;/a&gt;
            &lt;ul class="dropdown-menu" aria-labelledby="navbarSubDropdown"&gt;
                {% for ent in hideEntries.section('products').all() %}
                &lt;li&gt;&lt;a href="{{ cat.url }}/{{ ent.slug }}" class="dropdown-item" style="color:grey"&gt;{{ ent.title }}&lt;/a&gt;&lt;/li&gt;
                {% endfor %}

                &lt;!-- check to see if sub categories exist --&gt;
                {% if childCats|length %}
                    {% for subCat in childCats.all() %}

                        &lt;!-- check to see if there are entries within the sub categories --&gt;
                        {% set subEntries = craft.entries.section('products').relatedTo(subCat).all() %}
                        {% if subEntries|length %}
                        &lt;li class="dropdown-submenu dropdown"&gt;
                            &lt;a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;{{ subCat.title }}&lt;/a&gt;
                            &lt;ul class="dropdown-menu" aria-labelledby="navbarSubDropdown"&gt;
                                {% for entry in subEntries %}
                                    &lt;li&gt;&lt;a href="{{ cat.url }}/{{ subCat.slug }}/{{ entry.slug }}" class="dropdown-item"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
                                {% endfor %}
                            &lt;/ul&gt;
                        &lt;/li&gt;
                        {% else %}
                        &lt;!-- if no entries then just output the child categories --&gt;
                        &lt;li&gt;&lt;a href="{{ subCat.url }}" class="dropdown-item"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
                        {% endif %}


                    &lt;/li&gt;
                    {% endfor %}
                {% endif %}
            &lt;/ul&gt;
        &lt;/li&gt;

        {% else %}

            &lt;!-- check to see if there are sub category entries, if so, output them in a dropdown --&gt;
            {% set subEntries = craft.entries.section('products').relatedTo(cat).all() %}
            {% if subEntries|length %}
            &lt;li class="dropdown-submenu dropdown"&gt;
                &lt;a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;{{ cat.title }}&lt;/a&gt;
                &lt;ul class="dropdown-menu" aria-labelledby="navbarSubDropdown"&gt;
                    {% for entry in subEntries %}
                        &lt;li&gt;&lt;a href="{{ cat.url }}/{{ entry.slug }}" class="dropdown-item"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
                    {% endfor %}
                &lt;/ul&gt;
            &lt;/li&gt;
            {% else %}
            &lt;!-- if no entries, then just show the top level categories --&gt;
            &lt;li&gt;&lt;a href="{{ cat.url }}" class="dropdown-item"&gt;{{ cat.title }}&lt;/a&gt;&lt;/li&gt;
            {% endif %}

        {% endif %}

    &lt;/li&gt;

{% endfor %}

&lt;/ul&gt;
</code></pre>

CC BY-SA 4.0
2019-10-02T23:07:05.320Z
2019-10-02T23:07:05.320Z
xenowebdev
<p>These two pages should help you from the Craft 3 docs</p>

<p><a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto</a> </p>

<p><a href="https://docs.craftcms.com/v3/relations.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/relations.html</a></p>

CC BY-SA 4.0
2019-09-29T22:00:44.840Z
Thanks for posting the links but I've already been on the docs and am still stuck hence my post. Im still pretty new to Craft so would appreciate someone helping with the correct syntax so I can learn from here :)
xenowebdev
CC BY-SA 4.0
2019-09-27T07:21:16.460Z
2019-09-27T07:21:16.460Z
32423hjh32423
<p>I need some help with syntax on this for Craft 3, please.</p>

<p>I want to use this piece of code: </p>

<pre><code>{% set fruitEntries =  craft.entries.relatedTo({ relatedTo: catP,id: 'and, not '~str }) %}
</code></pre>

<p>from this answer here:</p>

<p><a href="https://craftcms.stackexchange.com/questions/11037/how-to-get-entries-related-to-current-category-only-not-children?rq=1">How to get entries related to current category only (not children)</a></p>

<p>It's exactly what I need to do but I'm not sure how to code it for Craft 3 so that I can pull in entries from JUST my 'products' section:</p>

<pre><code>craft.entries.section('products')...
</code></pre>

<p>Thanks in advance.</p>

<hr>

<p>UPDATED INFO.</p>

<p>Category Group = Products</p>

<p>Entry Sections using these categories = Products &amp; Case Studies</p>

<p>My category structure is as follows:</p>

<p>Categories are:</p>

<ul>
<li>Travelling Irrigators

<ul>
<li>Superseeded Models</li>
</ul></li>
<li>Hydrants</li>
<li>Filters</li>
<li>Pontoons</li>
<li>Pumps</li>
</ul>

<p>I'm trying to return just the product entries (not case studies) under 'Travelling Irrigators' but not including the 'Superseeded Models'.</p>

<p>The other post does exactly what I need but it returns the case studies too.</p>

CC BY-SA 4.0
2019-09-30T21:00:32.513Z
What category are you looking to retrieve entries from? How is it linked to the product entries?
Seb
<entries><categories><relations>
Craft 3 version - How to get entries related to current category only (not children)
# Craft 3 version - How to get entries related to current category only (not children)

(Asked by: username_0 on 2019-09-26)

I need some help with syntax on this for Craft 3, please.
I want to use this piece of code: 
{% set fruitEntries =  craft.entries.relatedTo({ relatedTo: catP,id: 'and, not '~str }) %}

from this answer here:
How to get entries related to current category only (not children)
It's exactly what I need to do but I'm not sure how to code it for Craft 3 so that I can pull in entries from JUST my 'products' section:
craft.entries.section('products')...

Thanks in advance.

UPDATED INFO.
Category Group = Products
Entry Sections using these categories = Products & Case Studies
My category structure is as follows:
Categories are:

Travelling Irrigators


Superseeded Models

Hydrants
Filters
Pontoons
Pumps

I'm trying to return just the product entries (not case studies) under 'Travelling Irrigators' but not including the 'Superseeded Models'.
The other post does exactly what I need but it returns the case studies too.

**Tags:** entries, categories, relations
> **Comments:**
> **username_1**: What category are you looking to retrieve entries from? How is it linked to the product entries?  

---

## 2 answers

### Answer by username_0 on 2019-10-02. Score: 0 | Accepted answer

So I finally got there in the end and am posting in case it helps anyone else.
I was building a drop down menu with bootstrap that showed a 2 tier category / entry menu.
Code as follows:


{% set catsL1 = craft.categories().group('products').level(1).all() %}    
{% for cat in catsL1 %} 

    {% set childCats = craft.categories.descendantOf(cat) %}
    {% set catParent = craft.categories.id(cat).one() %}
    {% set dontInclude = [] %}
    {% for child in craft.categories.descendantOf(cat).all() %}
        {% set dontInclude = dontInclude|merge(craft.entries.section('products').relatedTo(child).ids) %}
    {% endfor %}
    {% set str = dontInclude|join(', not ') %}
    {% set hideEntries = craft.entries({ relatedTo: cat,id: 'and, not '~str }) %}

        {% if hideEntries|length %}

        
        
            {{ cat.title }}
            
                {% for ent in hideEntries.section('products').all() %}
                {{ ent.title }}
                {% endfor %}

                
                {% if childCats|length %}
                    {% for subCat in childCats.all() %}

                        
                        {% set subEntries = craft.entries.section('products').relatedTo(subCat).all() %}
                        {% if subEntries|length %}
                        
                            {{ subCat.title }}
                            
                                {% for entry in subEntries %}
                                    {{ entry.title }}
                                {% endfor %}
                            
                        
                        {% else %}
                        
                        {{ entry.title }}
                        {% endif %}


                    
                    {% endfor %}
                {% endif %}
            
        

        {% else %}

            
            {% set subEntries = craft.entries.section('products').relatedTo(cat).all() %}
            {% if subEntries|length %}
            
                {{ cat.title }}
                
                    {% for entry in subEntries %}
                        {{ entry.title }}
                    {% endfor %}
                
            
            {% else %}
            
            {{ cat.title }}
            {% endif %}

        {% endif %}

    

{% endfor %}


---

### Answer by username_2 on 2019-09-27. Score: 1

These two pages should help you from the Craft 3 docs

<https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto>

<https://docs.craftcms.com/v3/relations.html>

> **Comments:**
> **username_0**: Thanks for posting the links but I've already been on the docs and am still stuck hence my post. Im still pretty new to Craft so would appreciate someone helping with the correct syntax so I can learn from here :)  

---

