Why does this ACF field not update properly?

90 Views Asked by At

I'm using Timber and Twig for a Wordpress site that I'm currently working on, but got stuck in this (for me) quite weird behaviour of an ACF field.

The ACF field is a simple true/false checkbox and in my template i want to do a conditional render depending on the fields value, which works. But when i update the ACF field in wp-admin, that post just not render.

I'm getting all the posts of a certain post type and in the template i check the value of the ACF on the post. If i for example create a new post and set the ACF-field it works, but when changing it, it wont render. What am i doing wrong here?

PHP

$context = Timber::context();

$context['coworkers'] = Timber::get_posts([
  'post_type' => 'coworker'
]);

$templates = array('page/coworkers/index.twig');

Timber::render($templates, $context);

Twig

{% if coworkers %} // list of posts

  {% for item in coworkers %}

    {% set coworker = item.meta('coworker') %} // 1 or 0

    {% if coworker %}
      <div>
          coworker
      // its a coworker
      </div>
    {% else %}
      <div>
          something
      // its something else
      </div>
    {% endif %}

  {% endfor %}

{% endif %}
1

There are 1 best solutions below

0
Math On

Solution below in php. Limit the query instead of having to use if/else in the template.

$context = Timber::context();

$context['coworkers'] = Timber::get_posts([
  'post_type' => 'coworker',
  'meta_query' => array(
      array(
          'key'   => 'coworker',
          'value' => '1',
      )
  )
]);

$templates = array('page/coworkers/index.twig');

Timber::render($templates, $context);