The majority of child custody and visitation disputes involve the parents. However, there are cases where someone other than either parent can obtain custody if there is sufficient evidence that it is in the best interests of the child for the court to do so.
Third party custody or visitation is limited only to "person[s] with a legitimate interest," which is defined in the Code of Virginia § 20-124.1 as "includes, but is not limited to grandparents, stepparents, former stepparents, blood relatives and family members provided any such party has intervened in the suit or is otherwise properly before the court," and is "broadly construed to accommodate the best interest of the child." The person seeking custody has to show that the child's parents or legal guardians are unfit by clear and convincing evidence, which is a high burden of proof. If the court determines that he or she has standing to challenge parental preference, it will treat the person as co-equal with the parent(s) in proceedings going forward. In other words, the third party "person with a legitimate interest" will have an fair shot to show the court that it is in the child's best interest to grant custody to someone other than either of the parents.
There are a number of court cases further defining what "legitimate interest" entails. A nice summary of these cases and more can be found here.