Answers.Body: <p>The cache tag is somewhat misunderstood and it tends to trip up even the most seasoned Crafters.</p>
  
  <p>The best way to explain about the <a href="https://docs.craftcms.com/v3/dev/tags/cache.html#parameters" rel="nofollow noreferrer"><code>{% cache %}</code></a> tag is to think of it as a snapshot in time. </p>
  
  <p>The first time the template runs, Craft saves the <strong>output</strong> to the database. It <a href="https://craftcms.stackexchange.com/questions/12445/caching-queries-not-working-as-expected">does not actually save queries</a>.  On subsequent runs, Craft simply grabs the output from the cache. Again, it doesn't save state or variables, it simply saves the output.</p>
  
  <p>Specifically, I see you have <code>{% if %}</code> tags inside <code>{% cache %}</code>. Those conditionals will only run once. The <code>{% if %}</code> tags won't re-execute and the output is going to be the same.</p>
  
  <p>What you can do is something like:</p>
  
  <p><code>{% if whatever %} 
        {% cache %} 
           {{ do some output stuff }}
        {%endcache%}
  {% endif %}</code></p>
  
  <p>For more info, check out <a href="https://straightupcraft.com/events/the-cache-tag" rel="nofollow noreferrer">Straight UP Craft</a> as well as Andrew Welch's <a href="https://nystudio107.com/blog/the-craft-cache-tag-in-depth" rel="nofollow noreferrer">great piece</a> on the subject. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T22:42:43.190Z
Answers.Text: I've done some further testing and I think I'm finding that the `id` param array might be part of my issue. If I remove `.id(['not']|merge(excludeIds))` it seems to be working as expected. Any thoughts?
Answers.UserDisplayName: Michael
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T20:25:33.797Z
Answers.LastActivityDate: 2019-01-11T20:25:33.797Z
Answers.UserDisplayName: RitterKnight
Body: <p><strong>-- UPDATED --</strong></p>
  
  <p>My understanding of how the cache busting works is to make sure the query is within the <code>{% cache %}</code> 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.</p>
  
  <p>The goal with this example is to show posts on the index and any category page that excludes <code>cateogry-one</code> unless a user is viewing the category one page.</p>
  
  <p>If I have an entry that is initially not tagged with <code>category-one</code> 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.</p>
  
  <p>Blog Index Code:</p>
  
  <pre><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 %}
  
          &lt;p&gt;Sorry, no posts found.&lt;/p&gt;
  
      {% endif %}
  
  {% endcache %}
  </code></pre>
  
  <p>Blog Category Code:</p>
  
  <pre><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 %}
  
          &lt;p&gt;Sorry, no posts found.&lt;/p&gt;
  
      {% endif %}
  
  {% endcache %}
  </code></pre>
  
  <p>I have had success with the busting the cache for the category page when I use the following code, which does not include <code>id: ['not']|merge(excludeIds)</code></p>
  
  <p>Any insight about the cache tag and if the <em>id not array</em> would be really helpful.</p>
  
  <p><strong>NOTE:</strong>
  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.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-11T19:50:22.903Z
Comments.Text: I should note that for the Blog Category Code that doesn't bust the cache, it does bust the cache for Category One, but not any other category pages.
Comments.UserDisplayName: Michael
Tags: <craft3><categories><cache>
Title: Excluding Array of Entry Ids Not Busting Cache
ThreadText: # Excluding Array of Entry Ids Not Busting Cache
  
  (Asked by: username_0 on 2019-01-11)
  
  **-- 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 @username_1 but this did not fix the cache issue related to excluding entry ids.
  
  **Tags:** craft3, categories, cache
  
  > **Comments:**
  > **username_0**: I should note that for the Blog Category Code that doesn't bust the cache, it does bust the cache for Category One, but not any other category pages.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-11. Score: 1
  
  The cache tag is somewhat misunderstood and it tends to trip up even the most seasoned Crafters.
  
  The best way to explain about the [`{% cache %}`](https://docs.craftcms.com/v3/dev/tags/cache.html#parameters) tag is to think of it as a snapshot in time.
  
  The first time the template runs, Craft saves the **output** to the database. It [does not actually save queries](https://craftcms.stackexchange.com/questions/12445/caching-queries-not-working-as-expected). On subsequent runs, Craft simply grabs the output from the cache. Again, it doesn't save state or variables, it simply saves the output.
  
  Specifically, I see you have `{% if %}` tags inside `{% cache %}`. Those conditionals will only run once. The `{% if %}` tags won't re-execute and the output is going to be the same.
  
  What you can do is something like:
  
  `{% if whatever %}
  {% cache %}
  {{ do some output stuff }}
  {%endcache%}
  {% endif %}`
  
  For more info, check out [Straight UP Craft](https://straightupcraft.com/events/the-cache-tag) as well as Andrew Welch's [great piece](https://nystudio107.com/blog/the-craft-cache-tag-in-depth) on the subject.
  
  > **Comments:**
  > **username_0**: I've done some further testing and I think I'm finding that the `id` param array might be part of my issue. If I remove `.id(['not']|merge(excludeIds))` it seems to be working as expected. Any thoughts?  
  
  ---
  
  