Home Forums ACF PRO Disable specific (select2) options from a Post Object field


Disable specific (select2) options from a Post Object field

  • First of all, below I’m referring to the backend side of WP. I’ve set-up my ACF Pro as described below:

    I’ve used a Repeater field, inside of which there is (among others) a Post Object field pulling data from a specific CPT. The inserted data in the CPT are top objects with one or some more child objects, ie:

    — Object4_Child1
    — Object4_Child2
    — Object4_Child3

    What I need is to prevent the admin from selecting top options (Object1, Object2, etc), but rather choose only from their children (Object1_Child1, Object1_Child2, etc).

    At first I thought I could use:

    add_filter('acf/fields/post_object/result/name={field_name}', 'my_acf_fields_post_object_result', 10, 4);

    to disable options where $post->post_parent equals to 0. But unfortunately the filter isn’t designed to add the functionality I want.

    After A LOT of searching, I found out that (being in the backend side of WP) I could use:

    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');

    inside of which I could use:

    acf.add_filter('select2_ajax_results', function( json, params, instance ){
    // do something to json
    return json;

    which at least contains a list (json param) of the AJAX populated options. So, json.results contains an array of objects with the properties id and text of the populated options. From there, I was able to use something like this (for test purposes, that disables all options):

    json.results.forEach(function (element) {
      element.disabled = "true";

    which adds a third property for each object (disabled: true), which is a built-in way to disable an option in select2. But, being in JS, I can’t/don’t know how to find only the top categories to disable (with property parent: 0). I think the only way to do that is going one step before, where json param is being constructed, to add a third property, like parent: [cpt parent id]. If that’s possible, then I could compare inside the filter ‘select2_ajax_results’ if parent: 0 to easily add the needed property disabled: true to those top options only. But is that even possible?

  • Any news here? I have a similar situation.

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

You must be logged in to reply to this topic.