Home Forums Feedback Custom data for post_object via prepare_for_ajax


Custom data for post_object via prepare_for_ajax

  • Hi folks!

    I didn’t know, which forum is the right one for this (and it seems there is no Github repo), so please move the thread if needed. 🙂

    As I was trying to pass custom data to the acf/fields/post_object/query filter I stumbled upon the prepare_for_ajax JS filter. So I’ve done the following in JS:

    acf.add_filter('prepare_for_ajax', function (args) {
      args.post__not_in = [1,2,3,4];
      return args;

    This will pass the post__not_in argument correctly to the ajax_query() in post_object.php. From there via $_POST to the get_choices method.

    Now there’s the part, which seems a bit weird to me, as the passed arguments can’t be used further.

    The method get_choices accepts one argument $options. These options are getting parsed via acf_parse_args() but the keys of the $options array are never used again just for filling a handful of fix args. Instead, a new array $args is created and filled with the results from the previous options array. Instead of creating a new $args array, the $options array could have be named $args and everything should work as expected (except for the “polluting variables” like field_key or post_id).

    Then you could adjust the args via JS instead of the need for another filter. – But maybe this will result in confusion as you don’t really expect the WP_Query args are set via JS.

    The other solution would be (maybe faster and less code-changing) to pass the $options array as the fourth argument in the filters.

    // filters
    $args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id'], $options);
    $args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id'], $options);
    $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id'], $options);

    Then I’m able to adjust everything as needed.

    These are my thoughts about this. I would be happy to get some feedback 🙂


  • Hi @fabianmarz,

    Thanks for the feedback.

    I would however recommend that you open a new ticket at [email protected] so as to reach Elliot faster since he rarely monitors the forum.

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

The topic ‘Custom data for post_object via prepare_for_ajax’ is closed to new replies.