1

I am attempting to list out all jobs that the current author has created but which are not already to other entries they've created.

I have both an array and a string containing the entry ids to exclude which I can see with these tags:

{# delete later, only here to visually show entry ids while coding #}
<p><strong>array:</strong> {{ excludeRelatedJobs|join(', ') }}</p>

{# convert array to string #}
{% set excludedJobs = excludeRelatedJobs|join(', ') %}

<p><strong>string:</strong> {{ excludedJobs }}</p>

However when I try to exclude the ids from the list they are not excluded.

current code that should exclude ids:

{% set careers = craft.entries.section('careers').authorId(user.id).id('not' ~ excludedJobs) %}
<ul>
    {% for job in careers %}
    <li><a href="/careers/edit/{{ job.id }}">{{ job.title }}</a></li>
    {% endfor %}
</ul>

If I manually exclude the id like this it works correctly:

{% set careers = craft.entries.section('careers').authorId(user.id).id('not 581') %}

  • 1
    I think that this question is related to your problem: craftcms.stackexchange.com/questions/1758/… – Youniteus Aug 14 '17 at 18:53
  • I based my code on that post. but it wasn't working – CreateSean Aug 14 '17 at 19:48
  • Seems you need to have an "and" in there: … id('and, not' ~ excludedJobs) – KSP Aug 14 '17 at 19:58
  • I did have that but when I do it doesn't list anything at all even when it should. – CreateSean Aug 14 '17 at 20:31
1

As @Youniteus stated in a comment to this post the answer was related to this question

I had to tweak the join statements a bit but it's working now.

complete code here:

{# convert array to string  so we can remove these ids #}
{% set excludedJobs = excludeRelatedJobs|join(', not ') %}

{# set careers to all jobs created by author#}
{% if not excludedJobs|length%} 
    {% set careers = craft.entries.section('careers').authorId(user.id) %}

{# filter out jobs already added to store#}
{% else %}
    {% set careers = craft.entries.section('careers').authorId(user.id).id('and, not ' ~ excludedJobs) %}
{% endif %}

<ul>
    {% for job in careers %}
    <li><a href="/careers/edit/{{ job.id }}">{{ job.title }}</a></li>
    {% endfor %}
</ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.