Support

Account

Home Forums Bug Reports wp_reset_postdata and acf/load_field issues

Solved

wp_reset_postdata and acf/load_field issues

  • As per the title, I’m experiencing a very peculiar issue. I’m using the acf/load_field filter to populate a few custom field select menus with posts. I’m coming across issues however, where wp_reset_postdata after the loop I run isn’t resetting the post loop, and subsequent uses of the $post global is showing the incorrect data.

    Here’s a sample of what my code looks like.

    
    
    add_filter('acf/load_field/name=event_venue_select', array($this->aidee_events_post_management, 'populate_venue_select'));
    
           function populate_venue_select ($field) {
                    
                global $post; 
                
                if ($post->post_type=='acf-field-group') return $field; 
                
                // Because ACF custom field management breaks if we return values dynamically. Since we don't need to populate them anyways, we 
                // just return the empty field instead.  
                
                $query_args = array(
                'post_type'  => strtolower($this->cpt_vars['Venue']),
                'paged' => 0,
                'posts_per_page' => -1,
                'post_status' => 'publish',
                
                );
    
                $venues = new WP_Query($query_args);
    
                $field['choices'] = array();
                
                if($venues->have_posts()) : while ($venues->have_posts()) : $venues->the_post();
                    
                    $field['choices'][get_the_id()] = get_the_title(get_the_id());
                
                endwhile;endif;   
                
                wp_reset_postdata();
                            
                return $field;
                    
            }
    
    

    I have a few filters with similar code. It seems that after the first filters hit, the $post global is fine. However subsequent filters end up with the $post global being modified by previous query loops, even though I wan wp_reset_postdata();

    If it helps any, all filters call methods within a class.

  • Hi @retribution

    How about manually resetting $post?

    
    add_filter('acf/load_field/name=event_venue_select', array($this->aidee_events_post_management, 'populate_venue_select'));
    
           function populate_venue_select ($field) {
                    
                global $post; 
                $org_post = $post;
                if ($post->post_type=='acf-field-group') return $field; 
                
                // Because ACF custom field management breaks if we return values dynamically. Since we don't need to populate them anyways, we 
                // just return the empty field instead.  
                
                $query_args = array(
                'post_type'  => strtolower($this->cpt_vars['Venue']),
                'paged' => 0,
                'posts_per_page' => -1,
                'post_status' => 'publish',
                
                );
    
                $venues = new WP_Query($query_args);
    
                $field['choices'] = array();
                
                if($venues->have_posts()) : while ($venues->have_posts()) : $venues->the_post();
                    
                    $field['choices'][get_the_id()] = get_the_title(get_the_id());
                
                endwhile;endif;   
                
                $post = $org_post;
                wp_reset_postdata();
                            
                return $field;
                    
            }
    
  • Thanks for the reply.

    Yeah, that’s the solution I ended up with. However, it just seems….hacky to me. And the little nagging coder voice inside me is pointing out “that’s not the right way to do it.” I think the real solution is to find out why the wp_reset_postdata() isn’t restoring the post object to it’s original state.

  • Yeah I suppose however I’ve seen that solution a fair amount of times before. I think it’s somewhere in the codex as well..

    But honestly since you’re just fetching the post title and ID you could do without setting up post_data at all..

    You could just loop over the $venues[‘posts’] in a foreach loop and output ->ID and ->post_title. Save yourself some function calls in the process.

  • Well instantiating a new query is enough to change the $post object, regardless of whether you iterate through it, or run it through a post loop. You’re right about that though, I have seen $org_post = $post in themes and templates so it is a solid “fix”. I’ll mark this as resolved, but it’s worth your team looking into why this behavior is occurring, as I have seen this issue in other threads here.

  • I will notify @elliot of this. He’s currently the sole developer, I just help out with the forums 🙂

    Good luck with your project and feel free to post if you have any new issues with ACF.

  • Yeah, I’m once again working with ACF and am still encountering this issue. It seems that if you try and run post loops within ACF, it messes with ACFS post object.

    The solution of copying/resetting the $post global still works.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘wp_reset_postdata and acf/load_field issues’ is closed to new replies.