After upgrading today to 5.2.9 (I think I was on 5.2.8) I’m having the following issue: I have a field (author) which is a User field, when the post is saved I update the post author to reflect the ACF field choice (the reasons I do this are not that important but basically gives me more control over the chosen author). All of a sudden when I hit save / publish my connection will timeout and the post doesn’t get saved. Commenting out the wp_update_post line from my function below fixes the problem but obviously means the post_author doesn’t get updated.
Any ideas? Thanks!
function author_acf_update_value($value, $post_id, $field) {
wp_update_post(array('ID' => $post_id,'post_author' => $value));
return $value;
}
add_filter('acf/update_value/name=author', 'author_acf_update_value', 10, 3);
Aha! The answer lies here: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops
Essentially I was creating an infinite loop of updating the value <–> updating the post. Not sure why this suddenly started happening (where it was working fine before), perhaps a WP or ACF change means that the update_value filter is getting triggered by wp_update_post where it wasn’t before.
Anyway, simple solution is:
remove_filter('acf/update_value/name=author', 'author_acf_update_value', 10);
wp_update_post(array('ID' => $post_id,'post_author' => $value));
add_filter('acf/update_value/name=author', 'author_acf_update_value', 10, 3);
I’m having the same problem which started when I updated to the new version of ACF.
However, even with the remove/add lines to stop looping, it’s still happening…
This is where I am right now; commenting out the wp_update_post makes the problem disappear…
I’d appreciate it if you could take a look and maybe see something obvious that I’ve missed!
//Auto add and update Title field:
function my_post_updater( $post_id ) {
// unhook this function to prevent infinite looping
remove_action( 'acf/save_post', 'my_post_updater' );
// change status if admin is user
if ( um_user( "role" ) == 'admin' ) {
$pStatus = "publish";
} else {
$pStatus = "draft";
}
// some variables to use
$pLocation = $_POST['acf']['field_554f18ecbf60c'];
$pLocation = strtoupper( $pLocation );
// get some tags of location + tags
// $pTags = $pLocation . ", " . $_POST['acf']['field_559924b3486da'];
$pTags = $pLocation;
$pTags = strtolower( $pTags );
// get the role & location & build the title
$pTitle = $pLocation . ": " . $_POST['acf']['field_559918d8fce21'];
// add the info at the end if it's locale_get_default
$extrainfo = "";
if ( $_POST['acf']['field_554f3827eee0c'] == 1 ) {
$extrainfo = "[L]";
}
if ( $_POST['acf']['field_55bf6059da1d8'] == "P" ) {
if ( $extrainfo == "[L]" ) {
$extrainfo = "[L,P]";
} else {
$extrainfo = "[P]";
}
}
if ( $_POST['acf']['field_55bf6059da1d8'] == "NP" ) {
if ( $extrainfo == "[L]" ) {
$extrainfo = "[L,NP]";
} else {
$extrainfo = "[NP]";
}
}
if ( $extrainfo != "" ) {
$pTitle = $pTitle . " " . $extrainfo;
}
// add the custom fields box onto the main content
$pContent = $_POST['acf']['field_55991581f79f7'] . "<p>[Custom Fields]</p>";
// Create the update
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $pTitle;
$my_post['post_status'] = $pStatus;
$my_post['post_content'] = $pContent;
$my_post['tags_input'] = $pTags;
// the permalink bit; begin by getting the slug
$post = get_post($post_id);
$slug = $post->post_name;
if ( is_numeric ( $slug ) ) {
// change to something more tangible otherwise leave alone
$my_post['post_name'] = $pTitle;
}
// Update the post into the database
wp_update_post( $my_post );
// send quick email to tell me
$pTitle = "New Post: " . $pTitle;
$pContent = do_shortcode( $pContent );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( "[email protected]", $pTitle, $pContent, $headers );
// rehook this function to prevent infinite looping
add_action( 'acf/save_post', 'my_post_updater' );
}
add_action('acf/save_post', 'my_post_updater', 20);
Not sure if it will fix it but I noticed that your remove_action call doesn’t specify the priority of the function. From the codex ( https://codex.wordpress.org/Function_Reference/remove_action ):
Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.
In other words try
remove_action( 'acf/save_post', 'my_post_updater', 20 );
That is awesome, Mooner! If we ever meet in real life, the beers are on me!
Can someone help me out on mine? I am having the same loop issue when I upgraded to 5.2.9. I was hoping it would be resolved in 5.3 or 5.3.1 but it wasn’t. So I’m throwing it out there for some assistance.
Thanks!
jeffrey
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$bluemovement = get_field('bluemovement');
if ( get_post_type() == 'bluemovement' ) {
$my_post['post_title'] = get_field('bmd_organization');
}
// Update the post into the database
wp_update_post( $my_post );
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
//END Auto add and update Title field:
//Auto add and update post content field:
function my_post_content_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$bluemovement = get_field('bluemovement');
if ( get_post_type() == 'bluemovement' ) {
$my_post['post_content'] = get_field('bmd_organization_desc');
}
// Update the post into the database
wp_update_post( $my_post );
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_content_updater', 20);
@jeffreyd00, have you tried removing and re-adding your actions as in the above solution?
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$bluemovement = get_field('bluemovement');
if ( get_post_type() == 'bluemovement' ) {
$my_post['post_title'] = get_field('bmd_organization');
}
remove_filter('acf/save_post', 'my_post_title_updater', 20);
// Update the post into the database
wp_update_post( $my_post );
add_action('acf/save_post', 'my_post_title_updater', 20);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
//Auto add and update post content field:
function my_post_content_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$bluemovement = get_field('bluemovement');
if ( get_post_type() == 'bluemovement' ) {
$my_post['post_content'] = get_field('bmd_organization_desc');
}
remove_filter('acf/save_post', 'my_post_content_updater', 20);
// Update the post into the database
wp_update_post( $my_post );
add_action('acf/save_post', 'my_post_content_updater', 20);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_content_updater', 20);
SOLVED!
Holy cow, that was really all there was to it?
Thanks so much. This does make me wonder what changed from 5.2.8 for this to suddenly start happening to so many uses.
I don’t know, to be honest I’ve never had the problem. Although I have not used it much I have used wp_update_post in an acf/save_post action.
In one that does not cause a problem the main difference I see is that I’m checking the post type near the beginning of the function. The other difference is that in most cases the problem seems to be with front end forms so there may have been a change in the way ACF processes front end forms.
@hube2
well it seems this did not solve my problem all it did was stop the function from working.
This is the code I have… if i remark out the ‘remove action’ I get the loop error. If put it back it never actually works.
//Auto add and update post content field:
function my_post_content_updater( $post_id ) {
// unhook this function to prevent infinite looping
remove_action('acf/save_post', 'my_post_content_updater', 20);
$my_post = array();
$my_post['ID'] = $post_id;
$bluemovement = get_field('bluemovement');
if ( get_post_type() == 'bluemovement' ) {
$my_post['post_content'] = get_field('bmd_organization_desc');
}
// Update the post into the database
wp_update_post( $my_post );
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_content_updater', 20);
Can we please poll Elliot on this as to why it all started with 5.2.9 ?
Infinite loops are extremely hard to debug, especially in WP with its hooks. Hooks interact with each other.
The best advice I can give you is to disable all the actions and filters you have added in your theme and then enable them one at a time until you find the one that causes the infinite loop. The next step is to disable all the actions and filters except that one and see if the infinite loop still happens. If not then start enabling them again until you locate the two that are causing it. You continue this process until you know all of the filters and actions that need to active to cause the infinite loop and then you start following the flow of code to figure out where the hooks are looping back on themselves.
This process can take hours, if not days, to locate the cause.
John,
I get that but it just seems weird that it happened to a handful of people when the version number changed. Granted my coding skills stink but it makes me think there is an error somewhere in ACF.
I don’t really find it weird. I actually find it odd that no this problem did not exist before.
I do know that for as long as I can remember the WP save_post
hook is when ACF saves your fields and this has always been done by do_action('acf/save_post')
which is why you can hook it both before and after the actual ACF save.
There may have been some changes in WP that happened at the same time that it started happening.
I think that most of the people having a problem with it’s with a front end form, but I could be wrong. For those people I would suggest investigating using the acf/pre_save_post
instead.
However, properly removing your action and then adding back in again should not cause the function to stop working completely. The function is already running when you remove it and when it’s added back in.
Here’s the link that the OP gave above https://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops
The topic ‘Update value function causes timeout’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.