When using the acf/load_field filter, problems can arise when a WP_Query object is used with a post loop. This commonly occurs when a field's options are being dynamically set to those of a post type or collection of posts.
To see me demonstrate and fix this problem, watch this video.
The problem stems from the query loop overriding core global variables that Advanced Forms relies on. When this happens, the form edit screen will fail to load any field groups and the error log will contain PHP warnings along the following lines:
PHP Warning: Trying to access array offset on value of type bool in /wp-content/plugins/advanced-forms/admin/admin-forms.php on line 117
The easiest way to fix this is to use a get_posts() call instead of a WP_Query(). e.g;
add_filter( 'acf/load_field/key=TARGET_FIELD_KEY_HERE', 'afp_test_acf_load_field_filter' ); function afp_test_acf_load_field_filter( $field ) { $field['choices'] = []; // Using get_posts() instead of WP_Query prevents query loop issues and global var overrides that break // functionality on the advanced forms edit screen and potentially other contexts. $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => - 1, 'orderby' => 'title', 'order' => 'ASC', ] ); foreach ( $posts as $post ) { $field['choices'][ $post->ID ] = get_the_title( $post ); } return $field; }
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article