I have a field group with family member details. It contains 2 relationship fields pointing at the same field group for the mother and father.
Is it possible to run a recursive CTE query to list the decendants?
Many thanks
Do you only have a relationship field that works in one direction, from children to parent?
The first suggestion I would make is to make the relationship fields bidirectional. There are several solutions for this, do a google search for “acf bidirectional relationship”
Thanks John,
Although it seems a good idea to change my configuration to a bidirectional relationship, that still doesn’t help with the idea of a recursive query. If I wanted to query a family tree showing a child, their parents, and the parents parents etc, I’d need some kind of recursive query.
Otherwise, I suppose I’d just have to do multiple queries in a recursive code block.
Regards
Pete
Using an ACF relationship field to get children
function recursive_children($parent_id) {
children = get_field('relationship_field', $parent_id);
if ($children) {
foreach ($children as $child) {
// do some stuff with child
// recurse this child
recursive_children($child->ID);
// do some other stuff with child
}
}
}