<p>You can get the posts related to your "entry" (the currently shown project) with a
<code>craft.entries</code> element criteria model where you set the <code>relatedTo</code> parameter like so:</p>

<pre><code>{% set relatedPosts = craft.entries.relatedTo({
    targetElement: entry
}) %}
</code></pre>

<p>Then check if this does find anything using Twig's <code>length</code> filter:</p>

<pre><code>{% if relatedPosts|length %}
    &lt;aside&gt;
        {% for relatedPost in relatedPosts %}
            {{ relatedPost.title }}
        {% endfor %}
    &lt;/aside&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-10-12T16:30:36.743Z
Thanks so much. I had actually worked out a way of getting the results but it was a much more convoluted way, with nested for loops. This was you don't even need to get the values of the entries field and then compare it to the title of the current entry, which is what I was doing. The above works perfectly!
Anna
CC BY-SA 3.0
2015-10-12T16:15:47.153Z
2015-10-12T16:15:47.153Z
carlcs
<p>I thought this would be simple but I am having no luck with it. I have a channel called "Posts" and for each Post there an "entry" custom field which lists all "Projects" (a structure). The user can select up to 3 of these within a Post. </p>

<p>The idea is to relate a current news Post with an existing project. In my Project entry template I basically want to check to see if the current project has been selected in the "relatedProjects" entry field. If it has, I need it to show a "Related Posts" title and then below that list each Post. </p>

<p>The thing I can't fathom out is how to have a section title that doesn't appear in the loop for each Post.</p>

<templating><relations>
Checking if current entry exists in an entry custom field in a different entry
# Checking if current entry exists in an entry custom field in a different entry

(Asked by: username_0 on 2015-10-12)

I thought this would be simple but I am having no luck with it. I have a channel called "Posts" and for each Post there an "entry" custom field which lists all "Projects" (a structure). The user can select up to 3 of these within a Post.

The idea is to relate a current news Post with an existing project. In my Project entry template I basically want to check to see if the current project has been selected in the "relatedProjects" entry field. If it has, I need it to show a "Related Posts" title and then below that list each Post.

The thing I can't fathom out is how to have a section title that doesn't appear in the loop for each Post.

**Tags:** templating, relations


---

## 1 answers

### Answer by username_1 on 2015-10-12. Score: 3 | Accepted answer

You can get the posts related to your "entry" (the currently shown project) with a
craft.entries element criteria model where you set the relatedTo parameter like so:
{% set relatedPosts = craft.entries.relatedTo({
    targetElement: entry
}) %}

Then check if this does find anything using Twig's length filter:
{% if relatedPosts|length %}
    
        {% for relatedPost in relatedPosts %}
            {{ relatedPost.title }}
        {% endfor %}
    
{% endif %}

> **Comments:**
> **username_0**: Thanks so much. I had actually worked out a way of getting the results but it was a much more convoluted way, with nested for loops. This was you don't even need to get the values of the entries field and then compare it to the title of the current entry, which is what I was doing. The above works perfectly!  

---

