Hi!
Is there any way to limit the input of items on post object fields that accept multiple posts? I noticed that this is a parameter that exists for relationship fields, but not for post objects, and I can’t understand why this is.
Is there anyway I can do this? Even if it means overriding select2’s options ?
Thanks in advance!
You could use a relationship field instead. A post object field that allows multiple values is just another way of displaying a relationship field.
Or you can create an acf/validate_value filter https://www.advancedcustomfields.com/resources/acf-validate_value/. $value in this case will be an array of post IDs and you can do
if (count($value) > $my_max_posts) {
$valid = 'No more than '.$my_max_posts.' allowed';
}
I’d also add a note to the field instructions about the maximum.
You could probably also do this using javascript and the select2_init hook to alter the select2 settings https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
Hi, and thanks for the answer!
I had already found the relationship field solution, but the type of input generated is not as adequate to the specific usage i’m trying to make of it as the post object’s select2.
The acf/validate_value solution isn’t too bad, but I’d prefer to actually not allow the introduction after the limit value is reached; and as for the select2_init hook, i’ve made some tests and i just don’t seem to get it to run – when putting instructions inside the select2_init add_action, it never seems to actually run, which is weird…
I guess it’s going to have to be the relationship field.
Thanks for your answer though!
Using custom JS, stuff that’s not listed in the docs
1) Enqueue your custom JS script on the acf/input/admin_enqueue_scripts hook
2) You custom script should depend on the acf script handle acf-input
3) You initialization code should look something like this
if (typeof acf != 'undefined') {
acf.add_action('select2_init', function($input, args, settings, $field) {
// $input (jQuery) hidden input element
// args (object) args given to the select2 function
// settings (object) settings given to the acf.select2 function
// $field (jQuery) field element
// you need to check the $field element to make sure your looking at the right field
// you can use the console to see what's in vars
// for example
conole.log($field);
});
}
I found this post on the select2 argument that you probably need to set http://stackoverflow.com/questions/19789984/limit-the-jquery-select2-maximum-selected-options
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 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.