Support

Account

Forum Replies Created

  • I have the same issue – ACF functions do not seem to work in update_post, regardless of priority.

    The solution for me, because I needed to use post_updated to compare old and new post titles and slugs, was to update the value in $_POST[‘acf’]. E.g:

    $_POST['acf']['field_5f19d4ad03a30'][] = ['field_5f19d4b503a31' => $old_title]

    In this case, I am saving the old title to a repeater. For this to work you must use field keys, not field names.

  • Thanks for this suggestion @hube2 – this is a nice piece of functionality.

    I made some tweaks for my use case, pasted here in case useful to anyone. In my case, I want to be able to check that the first field of each the repeater is unique – I don’t care what happens to the others. This way the field becomes a key.

    NB this doesn’t currently work for nested repeaters-in-repeaters. One day I may try to tackle that.

    The main function is here:

    add_filter('acf/validate_value/type=repeater', 'grly_acf_validate_repeater', 10, 4);
    
    function grly_acf_validate_repeater($valid, $value, $field, $input) {
    
    	if (!$valid || !$field['preventduplicates']) {
    		return $valid;
    	}
    
    	// get list of array indexes from $input
    	// [ <= this fixes my IDE, it has problems with unmatched brackets
    	preg_match_all('/\[([^\]]+)\]/', $input, $matches);
    	if (!count($matches[1])) {
    		// this should actually never happen
    		return $valid;
    	}
    	$matches = $matches[1];
    
    	// walk the acf input to find the repeater and current row
    	$array = $_POST['acf'];
    
    	$field_key = false;
    	$field_value = false;
    
    	for ($i = 0; $i < count($matches); $i++) {
    		if (isset($array[$matches[$i]])) {
    
    			$field_key = $matches[$i];
    			$field_value = $array[$matches[$i]];
    
    			if ($field_key == $field['key']) {
    				break;
    			}
    			$array = $field_value;
    
    		}
    	}
    
    	$used = [];
    	foreach ($field_value as $index => $row) {
    		$first_entry = reset($row); // Extracts the first entry in the object for that row;
    		if ($first_entry) {
    			if (!in_array($first_entry, $used)) {
    				$used[] = $first_entry;
    			} else {
    				$valid = 'The value ' . $first_entry . ' is used more than once. The first field in this repeater must be unique across the repeater.';
    				break;
    			}
    		}
    	}
    
    	return $valid;
    }
    

    … then I’ve added a ‘prevent duplicates’ field to all repeaters so that it can be toggled on and off:

    add_action('acf/render_field_settings/type=repeater', 'add_no_duplicates_field_to_repeater');
    function add_no_duplicates_field_to_repeater($field) {
    	acf_render_field_setting($field, [
    		'label' => __('Prevent duplicates?', 'acf'),
    		'instructions' => 'Prevents rows being saved where the first value is the same as that in another row',
    		'type' => 'radio',
    		'name' => 'preventduplicates',
    		'choices' => array(
    			1 => __("Yes", 'acf'),
    			0 => __("No", 'acf'),
    		),
    		'default_value' => 0,
    		'layout' => 'horizontal',
    	]);
    
    }
  • Cheers. You’re right, it was something else in my implementation causing the issue. It still works nicely. Thanks !

  • I’ve implemented the very helpful suggestion from @hube2 registering the read only settings to the settings field. Thanks!

    The only issue I have is that when creating posts programmatically the read only settings aren’t applied – this only happens after the first save in the admin screen.

    Is there a way I can force the setting to be applied when the post is created? Or can I manually trigger a save after post creation that would set them correctly?

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

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.