1

I am running into an odd issue where creating new child entries does not bust the cache of the parent page(s) that lists them.

This is the structure I am working with: enter image description here

I am displaying the Course entries on the Course Landing Pages like so:

{% cache using key craft.request.path %}

  {% set currentPage = craft.entries.id(entry.id).first() %}

  {% set entries = craft.entries({
    type: 'course',
    descendantOf: currentPage,
    with: [
      ['courseThumbnail', {
        withTransforms: [
          { mode: 'crop', width: 800, height: 800, position: 'center-center', format: 'jpg' }
        ]
      }]
    ]
  }) %}

  {% for entry in entries %}
      {% include '_includes/_listing-item-course' with { featured : true } %}
  {% endfor %}

{% endcache %}

When I publish a new Course entry, the cache for the parent page(s) is not busted and the new entry will not show unless I move/drag into a new position. This seems to clear the cache and the new entry shows on the parent page(s).enter image description here

If I change the craft.entries call to not include the descendantOf part, so it looks like this:

 {% set entries = craft.entries({
    type: 'course',
    with: [
      ['courseThumbnail', {
        withTransforms: [
          { mode: 'crop', width: 800, height: 800, position: 'center-center', format: 'jpg' }
        ]
      }]
    ]
  }) %}

Then newly added Course entries will show up straight away on all the Course Landing Pages.

Why is that?

1
  • There is definitely something going on with the descendantOf parameter. If I set entries like this {% set entries = craft.entries.type('course') %}, new ones show up straight away when published. The moment I change it to this {% set entries = craft.entries.type('course').descendantOf(entry) %}, they won't show up unless I manually clear the cache or move the new entry’s position in the structure tree. Very odd.
    – Katrin
    Commented Jun 10, 2017 at 12:46

3 Answers 3

2

This is a known issue – automatic cache busting currently doesn’t work when the descendantOf param is set to an actual element.

Unfortunately there’s no easy fix on Craft’s end, without a pretty drastic change to how cache busting works.

As a workaround, you can change your descendantOf param value to currentPage.id rather than just currentPage.

5
  • Thank you Brandon. I'm glad this is a known issue. However, using entry.id does not work for me either running version 2.6.2982. Any other workaround suggestions?
    – Katrin
    Commented Jun 10, 2017 at 14:23
  • I added my workaround solution as an answer. If you/anyone knows of a more efficient/elegant way, do let me know :)
    – Katrin
    Commented Jun 10, 2017 at 16:10
  • This also seems to apply when using entry.descendants or entry.getDescendants()? Commented Jul 3, 2017 at 13:49
  • And when using children()? Commented Jul 3, 2017 at 13:58
  • @TomDeSmet same Commented Jul 4, 2017 at 13:38
1

Since using descendantOf(currentPage.id), as Brandon suggested, didn't work, I have come up with this workaround solution:

{% set currentPageID = entry.id %}

{% set entries = craft.entries.type('course') %}

{# only display entries that have currentPage as an ancestor #}
{% for entry in entries if currentPageID in entry.getAncestors().ids() %}
  {% include '_includes/_listing-item-course' with { featured : true } %}
{% endfor %}
1

I found a better solution. I'm using the CacheFlag plugin which solves this problem. And it's very easy to add to existing code.

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.