Home › Forums › Feature Requests › 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.
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 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?
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 ;).
The topic ‘Relationship: Post Children Filter’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.