So after investigating a little, I was able to fix that.
The issue was that I was using a new custom post type called ‘order’, which seems to conflict with ACF (no idea why).
Changing my custom post type definition to something else ( ‘featuring_order’, in my case) solved the issue but it is really really strange…
Hi,
I am getting the exact same issue. Strangely, it did work properly for 3 months now, so I did create 22 fields groups as I do have various custom post type. It is becoming quite blocking, as I cannot get access to the field groups for modifications easily, and I am at the ending stage of the current project…
Almost all the plugins installed are custom.
ACF Pro: V 5.4.8
Wordpress V 4.5.3
Found a solution, reworked my code as the following:
jQuery(document).ready(function($) {
if (typeof acf === 'undefined') { return; }
var featuredContentExtension = acf.ajax.extend({
events: {
'change [data-key="field_5798cf2592b92"]': '_state_change'
},
_state_change: function(e) {
var select_field = $('#acf-field_5798ced492b91');
var number_field = $('#acf-field_5798cf2592b92');
var element = $('.acf-relationship');
var field_acf_data = acf.get_data( element );
var new_post_number = number_field.val();
select_field.attr('data-max', "[\"" + new_post_number + "\"]");
field_acf_data.max = new_post_number;
}
});
});
The key here was to retrieve the ACF data from the element using the acf.get_data
method, passing the relationship element as parameter 😉
Thanks for your attention !
Hum, I did try to change the value of the data-max attribute of the field on the fly, but it does not modify the max
property in the instance of the ACF field itself. My javascript code looks as follows:
jQuery(document).ready(function($) {
if (typeof acf === 'undefined') { return; }
var featuredContentExtension = acf.ajax.extend({
events: {
'change [data-key="field_5798cf2592b92"]': '_state_change'
},
_state_change: function(e) {
var select_field = $('#acf-field_5798ced492b91');
var number_field = $('#acf-field_5798cf2592b92');
var current_post_number = select_field.attr('data-max');
var selected_post_number = number_field.val();
select_field.attr('data-max', "[\"" + selected_post_number + "\"]");
}
});
});
I can see that the data-max attribute is properly updated, but the validation is still triggered based on the previous number selected when trying to select additional posts.
While looking at the implementation in assets/js/acf-input.js, I noticed the following code:
add_item: function( e ){
// max posts
if( this.o.max > 0 ) {
if( this.$values.find('.acf-rel-item').length >= this.o.max ) {
alert( acf._e('relationship', 'max').replace('{max}', this.o.max) );
return;
}
}
// rest of the method...
So, is there an easy way either to override or to hook into the add_item
method, or to modify the value of this.o.max
?
Thanks in advance,
I was able to do it in a static way using some code from another thread here
My current code looks like this
function load_proper_choices_number ( $field ) {
global $post;
if (!$post ||
!isset($post->ID) ||
( get_post_type($post->ID) != 'featured-content' ) ) {
return $field;
}
$number_value = get_post_meta($post->ID, 'featured_posts_number');
$field['max'] = $number_value;
return $field;
}
add_action( 'acf/load_field/name=featured_posts', 'load_proper_choices_number');
Works properly with a refresh every time I change the number of posts using the Number field, and now I will try to achieve it using some javascript. I do assume updating the data-max attributes of the relationship field should do the job.
Will let you know.
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.