Support

Account

Home Forums Backend Issues (wp-admin) How can I send an additional field during ajax requests? Reply To: How can I send an additional field during ajax requests?

  • @princeofabyss

    Do you have to show the parent posts in the field? If not then I would filter the query using acf/fields/post_object/query by adding $args['post_parent__in'] = array(0);

    If you must show the parents and have them disabled in the field, Unfortunately, there are no hooks that will let you filter the json response from the server. In this case what you would have to do is do a query to get a list of post IDs that have parent = 0 and output those as a JS variable so that you can compare the values returned from the request to determine disabled or not.

    
    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    function my_acf_input_admin_footer() {
        $args = array(
          'post_type' => 'your post type',
          'posts_per_page' => -1,
          'post_parent' => 0,
          'fields' => 'ids'
        );
        $parents = new WP_Query($args);
        ?>
        <script type = "text/javascript">
        (function($) {
            var parents = <?php echo json_encode($parents); ?>;
            // JS here
            acf.add_filter('select2_ajax_results', function(json, params, instance) {
                $.each($(json.results), function() {
    
                    // compare returned id with parents array here
    
                    $(this).prop("disabled", true);
                });
                return json;
            });
        })(jQuery);
        </script>
        <?php
    }