0

This is a category page. I am trying to display all entries from users with this specific category selected in their account (user field that is a category field type.) This is the code I have so far, but it is not returning results. No errors - just not returning anything unfortunately:

<h1>{{ category.title }}</h1>

{# Grab all the users who have this category selected (which is a 'category field type' user field) #}
{% set categoryUsers = craft.users.relatedTo(category).limit(null) %}

{# Grab all of the entries by those users #}    
{% set categoryUserEntries = craft.entries.section('jobs').relatedTo(categoryUsers).limit(null) %}

{% paginate categoryUserEntries.limit(8) as entries %}

     {% for entry in entries %} 
         <h1><a href="{{ entry.url }}">{{ entry.title }}</a></h1>
     {% endfor %}

     {% if paginate.prevUrl %}<a href="{{ paginate.prevUrl }}" class="pagination left">&laquo; Previous Page</a>{% endif %}
     {% if paginate.nextUrl %}<a href="{{ paginate.nextUrl }}" class="pagination right">Next Page &raquo;</a>{% endif %}

{% endpaginate %}

Thanks in advance!

  • Add a {{ categoryUserEntries|length }} before the pagination part. Does that return anything? – Brad Bell Oct 9 '14 at 17:48
  • This returned '0' although it should return 1 because 1 user has this category selected in the category user field. Testing the first variable - {{ categoryUsers|length }} - does give the correct amount so it looks like this is an issue with the way the 'relatedTo' is working for this. – Angela Oct 9 '14 at 21:56
1

I think I worked this out. It looks like the related entries need to be contained in the user loop. This is what I used to make this work:

{% set categoryUsers = craft.users.relatedTo(category) %}
    {% paginate categoryUsers.limit(1) as entries %}

        {% for categoryUser in entries %}
            {{ categoryUser.username }}

            {% set categoryEntry = craft.entries.section('jobs').authorId(categoryUser.id).first() %}       
                {% if categoryEntry %}
                    {{ categoryEntry.positionTitle }} (And other fields not added yet.)
                {% endif %}

             <p>Testing the total found: {{ categoryUsers|length }}</p>
        {% endfor %}

    {% if paginate.prevUrl %}<a href="{{ paginate.prevUrl }}" class="pagination left">&laquo; Previous Page</a>{% endif %}
    {% if paginate.nextUrl %}<a href="{{ paginate.nextUrl }}" class="pagination right">Next Page &raquo;</a>{% endif %}

    {% endpaginate %}

Still testing, but I think this is the solution. (Unless someone else has a better way of doing this. :))

| improve this answer | |

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.