Support

Account

Home Forums Feature Requests Relationship: Post Children Filter

Solving

Relationship: Post Children Filter

  • I have a recurring need to be able to filter the Relationship field based on the post’s (being edited) children.

    The acf/fields/relationship/query only accepts $args['post_parent'] = 0, not $args['post_parent'] = $post.

    I would love to see this being implemented, but at least it would be great if the relationship query filter could accept post_parent other than 0.

  • 
    function my_func_filter($args, $field, $post){
       $args['post_parent'] = $post->ID;
       return $args;
    }
    
    add_filter('acf/fields/relationship/query/name=YourACFFieldSlug', my_func_filter, 10,3);
    

    try this

  • Have tried this, it does not work. The filter only accepts 0 (i.e. returns only top-level pages). It does not take an ID.

  • Hmm, I just tested it on my site and all works fine

  • No go on my end. ACF Pro 5.0.7.

  • forgot some quotes =)
    maybe’ll help

    
    function my_func_filter($args, $field, $post){
       $args['post_parent'] = $post->ID;
       return $args;
    }
    
    add_filter('acf/fields/relationship/query/name=YourACFFieldSlug', 'my_func_filter', 10,3);
  • I’ve tried it exactly like that – doesn’t work.

  • I would like to second this – I am experiencing the same issues as @stiand is.

  • To clarify; I am sure the filter works (I can pass other arguments) – just the post_parent only accepts 0 as a value (seems like this anyways).

  • I would like to add that the same is the case for the post object field. Could someone please look into this?

  • I’m also having this issue

  • Being on ACF Pro 5.3.2.2 it still doesn’t seem to work. When I add it to my functions.php, and I really took care of it being proper, I only get a spinning loading animation, but no pages are being shown which I could select. If I leave off the filter, the field then works well (but of course showing all instead of only pages of the current page). Or did I miss anything here?

    Code is standard:

    function childpages_query( $args, $field, $post )
    {
        $args['post_parent'] = $post->ID;
    
        return $args;
    }
    
    add_filter('acf/fields/relationship/query/name=pages_childpages', 'childpages_query', 10, 3);

    Thanks for any help in advance ;).

  • Elliot finally got it going for me through personal support. If anyone is still interested, here is the code:

    /* ACF: limit child-page selection to current page’s children */
    
    function childpages_query( $args, $field, $post_ID )
    {
        $args['post_parent'] = $post_ID;
    
        return $args;
    }
    
    add_filter('acf/fields/relationship/query/name=pages_childpages', 'childpages_query', 10, 3);

    while pages_childpages is the relationship field. It finally works in my case ;).

  • Is it simply that it’s the post_ID that gets passed to the filter, and not the post object? And it only took 16 months …

  • It looks like it. The documentation seems to have been updated with it, too: http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/ .

    Is 16 months a long time on the interweb :)?

  • 16 months is long, yes. Things happen fast. For a website, it’s almost a lifetime.

  • I was kidding. Real hard, actually.
    I am just glad this has been solved, so I can pin on the next problems to solve them ;).
    Is this solution still of value to you, after such a long time?

  • It might be of value in the future, when/if I need this filter on a future project. Now I can’t even remember which site(s) I needed it for – it’s probably 25 projects ago.

  • You won’t even need to come here, since the documentation has the right code now as well ;). I have a private little database of code snippets put into Evernote, and I hurried to add this little thing to it for the next time I need it.

    You could probably mark my post with the code as the solution, so others coming here find it right away ;).

    Good luck with the next 25(0) projects then ;).

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

The topic ‘Relationship: Post Children Filter’ is closed to new replies.