Home › Forums › Bug Reports › acf/load_field alters $post?
Hello,
I am currently working on a site for a volleyballclub and the backend is stuffed with ACF fields. So far a good thing 🙂
They can add teams of their own and opponents and whenever they add a match they can pick a home and an away team. Those fields are ACF dropdowns filled with the load_field filter like this:
public function pre_populate_forms() {
# Fill the home and away team dropdowns
add_filter( 'acf/load_field/name=match_home_team', array( $this, 'pre_populate_teams_dropdown' ), 10, 3 );
add_filter( 'acf/load_field/name=match_away_team', array( $this, 'pre_populate_teams_dropdown' ), 10, 3 );
add_filter( 'acf/load_field/name=staff_name', array( $this, 'pre_populate_teams_staff_names' ), 10, 3 );
}
public function pre_populate_teams_dropdown( $field ) {
$field['choices'] = array();
$apollo_home_args = array(
'post_type' => 'teams',
'nopaging' => true,
);
$apollo_home_teams = new WP_Query( $apollo_home_args );
if( $apollo_home_teams->have_posts() ) :
while( $apollo_home_teams->have_posts() ) : $apollo_home_teams->the_post();
$field['choices']['Apollo'][get_the_ID()] = get_the_title();
endwhile;
wp_reset_postdata();
endif;
$apollo_away_args = array(
'post_type' => 'opponents',
'nopaging' => true,
);
$apollo_away_teams = new WP_Query( $apollo_away_args );
if( $apollo_away_teams->have_posts() ) :
while( $apollo_away_teams->have_posts() ) : $apollo_away_teams->the_post();
$field['choices'][__( 'Opponents', 'apollo' )][get_the_ID()] = get_the_title();
endwhile;
wp_reset_postdata();
endif;
return $field;
}
https://gist.github.com/YourMark/b7880476e7bd3d4087b6
So far so good. The fields are filled and the teams appear. But somewhere along the lines it looks like the load_field filter is altering the $post variable. I can not yet pinpoint exactely where yet, but when the post is saved, the $post variable suddenly consists of the last added team in the dropdown.
I was unable to find more examples of this, so maybe it’s a bug, maybe I am doing something wrong and maybe it’s something entirely new to you.
I am hoping you can help me out here. I am not sure weather I posted this in the right forum, so if it’s in the wrong place, feel free to move it.
Regards,
Mark
Little addendum:
If I var_dump the $post variable in pre_populate_teams_dropdown
I get this:
object(WP_Post)[8885]
public 'ID' => int 5084
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2014-02-25 10:23:54' (length=19)
public 'post_date_gmt' => string '0000-00-00 00:00:00' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string '' (length=0)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'auto-draft' (length=10)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'open' (length=4)
public 'post_password' => string '' (length=0)
public 'post_name' => string '' (length=0)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2014-02-25 10:23:54' (length=19)
public 'post_modified_gmt' => string '0000-00-00 00:00:00' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://loc.apollo8.nl/?post_type=matches&p=5084' (length=47)
public 'menu_order' => int 0
public 'post_type' => string 'matches' (length=7)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
object(WP_Post)[8714]
public 'ID' => int 4438
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2014-02-01 18:01:42' (length=19)
public 'post_date_gmt' => string '2014-02-01 17:01:42' (length=19)
public 'post_content' => string '' (length=0)
public 'post_title' => string 'Heren 1' (length=7)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'open' (length=4)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'heren-1' (length=7)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2014-02-01 18:01:42' (length=19)
public 'post_modified_gmt' => string '2014-02-01 17:01:42' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://loc.apollo8.nl/teams/heren-1/' (length=36)
public 'menu_order' => int 0
public 'post_type' => string 'teams' (length=5)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
So the first time it’s ok, but the second round it changes somehow.
And if I disable one of the dropdowns, it also seems to work ok. So this problem only occurs if both dropdowns are being pre_filled.
Hi @YourMark
Because your filter is creating a new WP_Query object, and using the function $apollo_home_teams->the_post();
, the global $post will be over written.
The best solutions is to not use WP_Query, but instead, use the get_posts
function and loop over the results like an array.
Thanks
E
Hello Elliot,
I changed it to:
publicic function pre_populate_teams_dropdown( $field ) {
global $post;
//var_dump( $post );
$field['choices'] = array();
$apollo_home_args = array(
'post_type' => 'teams',
'nopaging' => true,
);
$apollo_home_teams = get_posts( $apollo_home_args );
if( count( $apollo_home_teams ) > 0 ) :
foreach( $apollo_home_teams as $team ) {
$field['choices']['Apollo'][ $team->ID ] = get_the_title( $team->ID );
}
endif;
$apollo_away_args = array(
'post_type' => 'opponents',
'nopaging' => true,
);
$apollo_away_teams = geT_posts( $apollo_away_args );
if( count( $apollo_away_teams ) > 0 ) :
foreach( $apollo_away_teams as $team ) {
$field['choices']['Apollo'][ $team->ID ] = get_the_title( $team->ID );
}
endif;
return $field;
}
This works, however it strikes me as odd. The function get_posts uses WP_Query too and the wp_reset_postdata() filter makes sure the $post variable is restored. So somehow the $post variable isn’t reset the way it should.
public function pre_populate_teams_dropdown( $field ) {
$field['choices'] = array();
$apollo_home_args = array(
'post_type' => 'teams',
'nopaging' => true,
'fields' => 'ids',
);
$apollo_home_teams = new WP_Query( $apollo_home_args );
if( $apollo_home_teams->have_posts() ) :
while( $apollo_home_teams->have_posts() ) : $apollo_home_teams->the_post();
$field['choices']['Apollo'][$post] = get_the_title( $post );
endwhile;
wp_reset_postdata();
endif;
$apollo_away_args = array(
'post_type' => 'opponents',
'nopaging' => true,
'fields' => 'ids',
);
$apollo_away_teams = new WP_Query( $apollo_away_args );
if( $apollo_away_teams->have_posts() ) :
while( $apollo_away_teams->have_posts() ) : $apollo_away_teams->the_post();
$field['choices'][__( 'Opponents', 'apollo' )][$post] = get_the_title( $post );
endwhile;
wp_reset_postdata();
endif;
return $field;
}
Appears to work as well.
This remains a strange case. Somehow the wp_reset_postdata() isn’t working well. I’m still not sure weather it’s WP or ACF that’s messing up, but either one of them is. Perhaps something to look into.
The topic ‘acf/load_field alters $post?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.