Home Forums ACF PRO Max items for post object field


Max items for post object field

  • 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 $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

  • 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

    I found this post on the select2 argument that you probably need to set

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

The topic ‘Max items for post object field’ is closed to new replies.