Support

Account

Home Forums Backend Issues (wp-admin) Update fields in multisite 503 error

Helping

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;
    }
    
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Update fields in multisite 503 error’ is closed to new replies.