2

I have read through the docs and I am not sure if what I need to do is possible with array sets. I have filtered my entries using the following code"

{% set allDatabytes = craft.entries.section('databytes').limit(null) %}
{% set seriesCollection = [] %}

{% for databyte in allDatabytes %}

  {# get only the series databytes #}
  {% set series = databyte.series %}

  {# add the series databytes to the series collection #}
  {% for entry in series %}
    {% set seriesCollection = seriesCollection|merge([entry.title]) %}
  {% endfor %}

{% endfor %}

{# Output the filtered results #}

{% set filteredDatabytes = allDatabytes|without(seriesCollection) %}
<ul>
  {% for entry in filteredDatabytes %}
    <li>{{ entry.title }}</li>
  {% endfor %}
</ul>

What I now need to do is add pagination to the filteredDatabytes set of content.

From The Example Below

This is my code which currently doesn't display any results. I guess you add the limit to the MyCriteria model (is that the correct term)?

{% set MyCriteria = craft.entries.section('databytes').series(':empty:').limit(10) %}

{% paginate MyCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        <a href="{{ paginate.prevUrl }}">Previous Page</a>
    {% endif %}

    {% if paginate.nextUrl %}
        <a href="{{ paginate.nextUrl }}">Next Page</a>
    {% endif %}
{% endpaginate %}
3

As Victor rightfully points out, the {% paginate %} tags require an ElementCriteriaModel, so you can't do what you're doing in your first example because as soon as you call:

{% for databyte in allDatabytes %}

Craft hits the database and converts the ElementCriteriaModel into actual EntryModels.

We also don't have support, yet, for passing :empty: into things like AssetFields, TagFields, etc., which is why your second example won't work... however that is on our list to add!

In this case, I think your best bet is going to be to do through the search service with something like this:

{# Search for 10 entries in the 'databytes' section where the 'series' assetField doesn't have anything selected. #}
{% set myCriteria = craft.entries.section('databytes').search('-series:*').limit(10) %}

{% paginate myCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        <a href="{{ paginate.prevUrl }}">Previous Page</a>
    {% endif %}

    {% if paginate.nextUrl %}
        <a href="{{ paginate.nextUrl }}">Next Page</a>
    {% endif %}
{% endpaginate %}
| improve this answer | |
  • Hi Brad, thank you that worked great. Its good to see there is so many different ways to attack a problem. – Gareth Jul 9 '14 at 4:59
1

This won't work with your example, because the pagination-tag only accepts an ElementCriteriaModel, not an array of already fetched entries:

{% paginate MyCriteria as PageSubset %}

See also: What are the differences between an elementCriteriaModel and fetched elements?


If I understand your code correctly, you want to get all entries which have an empty series-field?

If so, you don't have to fetch all entries and check against it, you can just add this to the ElementCriteriaModel:

{% set MyCriteria = craft.entries.section('databytes').series(':empty:') %}

{% paginate MyCriteria as entries %}
....
{% endpaginate %}

Why does the pagination tag need a CriteriaModel?

An ElementCriteriaModel can be compared to a sql query, it specifies what elements/entries to fetch (it sets the criteria). If you treat your CriteriaModel as an array or append .first() it will fetch those entries and replace the results with the CriteriaModel.

The pagination-tag needs a CriteriaModel (or "query") because it adds an offset (to the "query") depending on what page you are on, and then fetches the entries. - That's why the pagination-tag doesn't work with already fetched entries.

| improve this answer | |
  • Hi Victor, thank you, that looks like just the solution I am after. I spent ages trying to figure out what I came up with to. Sorry but how Craft works with ElementCriteriaModels is till very confusing to me and I have read the docs and Stuarts post a few times. Could you expand on your example as the only pagination example I have is buildwithcraft.com/docs/templating/tags#paginate and I can't figure what to add to your solution to make it work. Many thanks for your help. – Gareth Jul 7 '14 at 13:40
  • I added an explanation for the CriteriaModel. Copying the example pagination-code from the docs inside my example should work fine, what problems do you have exactly? – Victor In Jul 7 '14 at 15:52
  • Hi Victor, thank you for the explanation and I have updated my example to show you what I have which currently displays no results. – Gareth Jul 7 '14 at 16:14
  • I tested your code and it worked in my test. What field type is series and what does it contain? – Victor In Jul 7 '14 at 16:26
  • series is an entries field type: buildwithcraft.com/docs/entries-fields and can contain multiple related entries. – Gareth Jul 7 '14 at 16:45

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.