(b) The court may appoint a guardian ad litem for a minor child in any action affecting the family if the child's legal custody or physical placement is stipulated to be with any person or agency other than a parent of the child or, if at the time of the action, the child is in the legal custody of, or physically placed with, any person or agency other than the child's parent by prior order or by