Support

Account

Home Forums ACF PRO ACF code sample breaks certain templates

Helping

ACF code sample breaks certain templates

  • I haven’t verified if this happens everywhere or not but I’ve seen a number of posts here complaining about an un-restored outer loop.
    Take for example the first code sample on http://www.advancedcustomfields.com/resources/relationship/ where the variable $posts is declared.
    I couldn’t for the life of me figure out why wp_reset_postdata() wasn’t working and worse, my main loop had been replaced with the inner loop. It turns out that WP_Query->next_post also deals with an array called $posts. So setting a variable by that name was clobbering the original somehow.
    I’m not sure this can be considered wordpress core issue as it should prevent you from declaring such a variable but I thought I’d make note here.

  • Took me a while to figure out what you were referring to, but yes, if you use the variable $posts as shown in that first code example it will overwrite the entire global posts array that WP uses to loop over the main query.

    I’ll mark this topic for the developer to take a look at when he has time. The documentation should probably be altered to avoid referring to other global values as well.

    The problem that you encountered could also be corrected (I think) by using wp_reset_query() in place of wp_reset_postdata() https://codex.wordpress.org/Function_Reference/wp_reset_query

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

The topic ‘ACF code sample breaks certain templates’ is closed to new replies.