1

I am trying to get results from a relatedTo query, but only if there are more than say 1 results. Right now I am using two queries to achieve this, like so:

{% set authors = craft.entries({
    section: 'authors',
    with: [
        'authorImage',
    ],
    limit: 18,
    order: 'authorSurname asc'
}) %}
{% for author in authors %}
    {% set relatedEntries = craft.entries.section('articles').sponsoredAuthor('= 0').relatedTo(author) %}
    {% if relatedEntries|length > 1 %} ... {% endif %}
{% endfor %}

But this messes up pagination (amongst other things) because the loop will iterate no mater if the related entries have more than 1 result. So I was wondering if it is possible (without using a custom query) to have the same result as a single query, so that the results match the number of the items in the loop and pagination does not break. Something like:

authors = craft.entries.section('authors').relatedTo('articles where articles results > 1)

or something like that. Is it possible?

Thanks!

4
  • Is the relationship field stored on the Authors or the Entries? What is its handle? Commented Jun 28, 2017 at 5:33
  • Hi Brandon, the relationship field is stored on the entries (relatedAuthor). Tried something like that relatedTo({ sourceElement: craft.entries.section('articles').sponsoredAuthor('= 1'), field: 'relatedAuthor' but still not sure how to get the length / results from Articles
    – Evangelos
    Commented Jun 28, 2017 at 6:56
  • OK and to clarify, you want to loop through all the authors that have at least 2 entries related to them? Commented Jun 28, 2017 at 11:02
  • Exactly! But on a single query, otherwise if I have to add a condition whether or not to show something (relatedEntries > 1) it messes up the pagination. I am also looking for a sponsoredAuthor (a field on articles) to be off (0) - here is our demo epixeiro.gr/authors, you can see many authors with 0 or 1 entries - I do not want to show these, but I still want the pagination (limit + offset entries) to work properly! Thanks.
    – Evangelos
    Commented Jun 28, 2017 at 12:37

1 Answer 1

1

The only way I can think to do this is with a plugin that tracks the relation count, storing it as a custom field.

First create a new Number custom field called “Total Articles” (handle: totalArticles). Assign it to your Authors field layout.

Then create a new plugin. Put this in the primary plugin class’ init() method:

craft()->on('entries.saveEntry', function(Event $event) {
    /** @var EntryModel $entry */
    $entry = $event->params['entry'];

    if ($entry->getSection()->handle === 'articles') {
        // Update author relation counts
        foreach ($entry->relatedAuthor as $author) {
            $criteria = craft()->elements->getCriteria(ElementType::Entry);
            $criteria->section = 'articles';
            $criteria->relatedTo(array(
                'targetElement' => $author,
                'field' => 'relatedAuthor'
            ));
            $totalArticles = $criteria->total();

            craft()->db->createCommand()->update('content',
                array('field_totalArticles' => $totalArticles),
                array('elementId' => $author->id));
        }
});

Finally, from your template, you can fetch only the authors that have at least 2 articles like so:

{% set authors = craft.entries({
    section: 'authors',
    totalArticles: '>= 2',
    with: [
        'authorImage',
    ],
    limit: 18,
    order: 'authorSurname asc'
}) %}
3
  • Thanks Brandon! It works, just had to change $criteria->relatedTo = array(...); But now I have to re-save all entries in order to get the proper counts. Is there a (mass) way to do this, without updating the "updated date"? Thanks!
    – Evangelos
    Commented Jul 12, 2017 at 11:30
  • @epap If you resave the section, all the entries will get resaved (even if you don't change any settings). Although I think that the dateUpdated columns will get updated, too. Commented Jul 12, 2017 at 16:36
  • What if I want to remove an author from an article. How to I update the totalArticles textfield. I cannot find an event that will help me grab the previous author (related entry) before I replace it (or remove it) - How do I approach this?
    – Evangelos
    Commented Jul 18, 2017 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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