Last updated on September 19, 2023

ACF dynamic content not updating

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Updating Dynamic Content with ACF.

ACF (Advanced Custom Fields) is a popular plugin for adding custom fields to WordPress. Sometimes, when using ACF, you may encounter an issue where the dynamic content is not updating as expected. This can be frustrating, but there are a few steps you can take to troubleshoot and resolve the issue.

One common reason for ACF dynamic content not updating is caching. If your site has a caching plugin or server-level caching enabled, it may be serving cached versions of your pages, which can prevent the dynamic content from updating. To resolve this, you can exclude the ACF fields from being cached.

Here’s an example code snippet that demonstrates how to exclude ACF fields from caching using the popular caching plugin, W3 Total Cache:

function wpsnippets_exclude_acf_from_cache( $ignored ) {
    global $post;

    if ( function_exists( 'get_field' ) && ! empty( $post ) ) {
        $acf_fields = get_field_objects( $post->ID );

        if ( $acf_fields ) {
            foreach ( $acf_fields as $field ) {
                $ignored[] = 'acf_' . $field['name'];
            }
        }
    }

    return $ignored;
}
add_filter( 'w3tc_refuse_to_cache_page', 'wpsnippets_exclude_acf_from_cache' );

This code snippet adds a filter to the w3tc_refuse_to_cache_page hook, which allows you to exclude specific elements from being cached by W3 Total Cache. It checks if the get_field_objects() function exists (indicating that ACF is active) and retrieves all ACF fields for the current post. Then, it adds each ACF field name to the $ignored array, which tells W3 Total Cache to exclude those fields from caching.

By using this code snippet, you can ensure that your ACF dynamic content is always up to date, even when caching is enabled.

Note: This code snippet assumes you have the W3 Total Cache plugin installed and activated. If you’re using a different caching plugin, you may need to modify the code accordingly.

Examples

Example #1: ACF Dynamic Content Not Updating – Cache Clearing

This use case demonstrates how to clear the cache when ACF dynamic content is not updating.

function wpsnippets_clear_acf_cache() {
    if (function_exists('acf_clear_local_cache')) {
        acf_clear_local_cache();
    }
}
add_action('acf/save_post', 'wpsnippets_clear_acf_cache');

The code example above shows a custom PHP function wpsnippets_clear_acf_cache() that clears the ACF cache using the acf_clear_local_cache() function. This function is hooked to the acf/save_post action, which triggers whenever an ACF field is saved. By clearing the cache on save, any dynamic content that relies on ACF fields will be updated immediately.

Example #2: ACF Dynamic Content Not Updating – Force Update

This use case demonstrates how to force an update of ACF dynamic content when it is not updating automatically.

function wpsnippets_force_acf_update($value, $post_id, $field) {
    if (empty($value)) {
        $value = get_field($field['name'], $post_id);
    }
    return $value;
}
add_filter('acf/load_value', 'wpsnippets_force_acf_update', 10, 3);

The code example above shows a custom PHP function wpsnippets_force_acf_update() that forces an update of ACF dynamic content. This function is hooked to the acf/load_value filter, which allows modifying the value of an ACF field before it is loaded. By checking if the value is empty and retrieving it again using get_field(), we ensure that the dynamic content is always up to date.

Example #3: ACF Dynamic Content Not Updating – Disable Caching

This use case demonstrates how to disable caching for ACF dynamic content when it is not updating.

function wpsnippets_disable_acf_caching($value, $post_id, $field) {
    wp_cache_delete($post_id, 'acf');
    return $value;
}
add_filter('acf/load_value', 'wpsnippets_disable_acf_caching', 10, 3);

The code example above shows a custom PHP function wpsnippets_disable_acf_caching() that disables caching for ACF dynamic content. This function is hooked to the acf/load_value filter, which allows modifying the value of an ACF field before it is loaded. By deleting the ACF cache for the specific post ID using wp_cache_delete(), we ensure that the dynamic content is always retrieved fresh without any caching interference.

Last updated on September 19, 2023. Originally posted on September 19, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *