Home Forums General Issues Trouble with acf/fields/post_object/query Filter


Trouble with acf/fields/post_object/query Filter

  • Hello,

    I am attempting to use a Post Object field to represent a relationship between Posts of the same type.

    I am using acf_form to render the form on the front end of my site.

    When I render an ‘Edit’ Form ( by passing the post_id in to acf_form) the post I am trying to edit appears in the Post Object Field Select, which is undesirable.

    I would like to use the acf/fields/post_object/query filter so my form only renders these posts.

    Here is a sample of the code I have tried:

    add_filter( 'acf/fields/post_object/query/name=my_field', [ $this, 'my_filter' ] );
    public function my_filter( $args, $field, $post_id ) {
        $field; // Undefined
        $post_id; // Undefined
         if(isset($post_id)) {
    		$args['post__not_in'] = [$post_id]; 
         return $args;

    The filter is called at the right time, but the $field and $post_id parameters are not set.

  • post__not_in should be an array, so that’s the first suggested change

    add_filter( 'acf/fields/post_object/query/name=my_field', [ $this, 'my_filter' ] );
    public function my_filter( $args, $field, $post_id ) {
    $field; // Undefined
    $post_id; // Undefined
    if(isset($post_id)) {
    $args['post__not_in'] = array([$post_id]);
    return $args;

    If this field is a sub field of a repeater or flex field you also need to use the field key for adding the filter.

  • In my example $post__not_in is an array. But this is not the issue.

    The filter is called at the right time, but the $field and $post_id parameters are not set. It is not a subfield of a repeater field.

  • sorry, I missed the obvious, and not talking about the array, I personally never use shorthand for anything so I missed that too.

    But what I missed is your add_filter() code. If you want more than 1 argument passed to your filter then you need to tell WP how many is should pass. In this case it should be 3

    add_filter( 'acf/fields/post_object/query/name=my_field', [ $this, 'my_filter' ], 10, 3 );
  • Thank you for your reply John,

    $post_id is now set, but it is always set to 0.

    Possibly this is because I am using AJAX to load the form as described in this link.

    I did some code tracing and here is the $_POST variable.

    action => "acf/fields/post_object/query",
    nonce => "c35b1d6bc1",
    paged => "1",
    s => "",
    field_key => "field_588bcb6648c75"

    Because post_id is not set it defaults to 0 as defined in acf_field_post_object->get_ajax_query()

    Is this by design?


    When I do this query from wp_admin, the post_id IS set.

  • Yes, I’d say that has something to do with it.

    I’m not sure what direction to go with that information. You somehow need to set the post id. Without knowing more about what you’re doing, and really just guessing here…

    well, I’ve started to type several suggestions and removed them. As I think about them as I start typing I run into road blocks. The main problem here is that the ajax request is being done by ACF. You somehow need to find a way to make ACF send the correct post ID during the request. I’ve done a lot with custom AJAX queries for ACF fields, but I’ve never been able to alter the AJAX requests that ACF is already doing.

    There is only one thing that I can think of and that would be to pass the post ID in your ajax request and use that to set the post ID value when calling acf_form(), and I haven’t got a clue if this will work. But if it does then maybe ACF will either pass the correct post ID or it might be available in $_POST.

  • I don’t remember that post or the comments that followed mine, but it was interesting. Hopefully I remember it the next time I’m looking for something similar.

  • Hi, could you tell me what is 10 & 3 from
    add_filter( 'acf/fields/post_object/query/name=my_field', [ $this, 'my_filter' ], 10, 3 );

  • 10 is the priority of the filter and 3 is the number of variables to be passed to the function.

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

The topic ‘Trouble with acf/fields/post_object/query Filter’ is closed to new replies.