Home › Forums › Backend Issues (wp-admin) › Update fields in multisite 503 error
I’m working on WP v5.4.1 on Multisite and ACF PRO v5.8.11.
I have 3 sites in my installation.
I’m using the update field filter to check when an input changes in site 1 and update the related field in the site 3:
add_filter('acf/update_value/key=field_5edcc11eec039', '_acf_update_value_key_field_5edcc11eec039', 10, 3);
function _acf_update_value_key_field_5edcc11eec039($post_value, $post_id, $field) {
$field_value = get_field('field_5edcc11eec039', $post_id);
if ($post_value !== $field_value) {
switch_to_blog(3);
if ($post_value) {
update_field('field_5edd04a1cc440', strval($post_id), intval($post_value) );
}
if ($field_value) {
update_field('field_5edd04a1cc440', '', intval($field_value) );
}
restore_current_blog();
}
return $post_value;
}
The code above works fine. The change of field_5edcc11eec039 on site 1, changes the value of field_5edd04a1cc440 on site 3.
Later I want to implement the vice-versa.
add_filter('acf/update_value/key=field_5edd04a1cc440', '_acf_update_value_key_field_5edd04a1cc440', 11, 3);
function _acf_update_value_key_field_5edd04a1cc440($post_value, $post_id, $field) {
$field_value = get_field('field_5edd04a1cc440', $post_id);
if ($post_value !== $field_value) {
switch_to_blog(1);
if ($post_value) {
update_field('field_5edcc11eec039', '79', 93 );
//update_post_meta( intval($post_value), 'field_5edcc11eec039', strval($post_id) );
}
if ($field_value) {
update_field('field_5edcc11eec039', '', 93 );
//update_post_meta( intval($field_value), 'field_5edcc11eec039', '' );
}
restore_current_blog();
}
return $post_value;
}
The same logic, just in opposite direction… the change of field_5edd04a1cc440 on site 3, should change values of field_5edcc11eec039 on site 1.
But the second filter just cause a server overload and gives 503 error.
This is a bug in the update_field bahaviour on multisite? Maybe some priviles issue?
I’m using the same theme for all my sites, and the same functions.php. Can be that related to the issue?
Regards
Because you are switching to blog 1 and then trying to update the field there, this is causing your first filter to run, the call to your first filter is then causing your second filter to run creating an infinite loop.
In each filter you must have the filter remove itself before trying to update the other field and then re-add the filter when your done. This must be done in both filters
example:
add_filter('acf/update_value/key=field_5edd04a1cc440', '_acf_update_value_key_field_5edd04a1cc440', 11, 3);
function _acf_update_value_key_field_5edd04a1cc440($post_value, $post_id, $field) {
// remove self
remove_filter('acf/update_value/key=field_5edd04a1cc440', '_acf_update_value_key_field_5edd04a1cc440', 11);
// preform filter operation
// re-add self
add_filter('acf/update_value/key=field_5edd04a1cc440', '_acf_update_value_key_field_5edd04a1cc440', 11, 3);
return $post_value;
}
You must be logged in to reply to this topic.
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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.