-- UPDATED --
My understanding of how the cache busting works is to make sure the query is within the {% cache %}
tag. When looking into how it works I may not really be understanding the ins and outs of using the cache tag properly. Correct me if I'm wrong.
The goal with this example is to show posts on the index and any category page that excludes cateogry-one
unless a user is viewing the category one page.
If I have an entry that is initially not tagged with category-one
and it will output as expected as well as when I tag that entry with that category. However if I were to again edit that entry and remove category one the cache doesn't bust and won't output the entry.
Blog Index Code:
{# Get all the categories you want to INCLUDE #}
{% set categoryIds = craft.categories()
.group('categories')
.slug('category-one')
.ids() %}
{% set excludeIds = craft.entries()
.relatedTo({
targetElement: categoryIds
})
.ids() %}
{% cache globally using key craft.app.request.pathInfo ~ '/p' ~ craft.app.request.pageNum %}
{% set blogEntriesCriteria = craft.entries({
section: 'blog',
id: ['not']|merge(excludeIds),
with: ([
['image', {
withTransforms: ['medium']
}]
])
}) %}
{% set isFirstPage = craft.app.request.pageNum() == 1 %}
{% set limit = 10 %}
{% paginate blogEntriesCriteria.limit(limit) as pageInfo, blogEntries %}
{% if blogEntries %}
{# Loop through this page's News entries #}
{% for entry in blogEntries %}
{% include 'includes/components/_news-card' %}
{% endfor %}
{% else %}
<p>Sorry, no posts found.</p>
{% endif %}
{% endcache %}
Blog Category Code:
{% set catIdCheck = false %}
{# Get all the categories you want to INCLUDE #}
{% set categoryIds = craft.categories()
.group('categories')
.slug('category-one')
.ids() %}
{% set excludeIds = craft.entries()
.relatedTo({
targetElement: categoryIds
})
.ids() %}
{% if category.id == categoryIds[0] %}
{% set catIdCheck = true %}
{% endif %}
{% cache globally using key craft.app.request.pathInfo ~ '/p' ~ craft.app.request.pageNum %}
{% if catIdCheck == true %}
{# If true show category-one entries
on Category One Page #}
{% set blogEntriesCriteria = craft.entries({
section: 'blog',
relatedTo: category,
with: ([
['image', {
withTransforms: ['medium']
}]
])
}) %}
{% endif %}
{% if catIdCheck == false %}
{# If false show all posts that
do not include category-one on all other
category pages #}
{% set blogEntriesCriteria = craft.entries({
section: 'news',
relatedTo: category,
id: ['not']|merge(excludeIds),
with: ([
['image', {
withTransforms: ['medium']
}]
])
}) %}
{% endif %}
{% set isFirstPage = craft.app.request.pageNum() == 1 %}
{% set limit = 10 %}
{% paginate blogEntriesCriteria.limit(limit) as pageInfo, blogEntries %}
{% if blogEntries %}
{# Loop through this page's News entries #}
{% for entry in blogEntries %}
{% include 'includes/components/_news-card' %}
{% endfor %}
{% else %}
<p>Sorry, no posts found.</p>
{% endif %}
{% endcache %}
I have had success with the busting the cache for the category page when I use the following code, which does not include id: ['not']|merge(excludeIds)
Any insight about the cache tag and if the id not array would be really helpful.
NOTE: I have updated my Blog Category file to have the if statements outside of the cache tag recommended by @RitterKnight but this did not fix the cache issue related to excluding entry ids.