Set transform of a child object to an arbitrary point using a script
Use Vector maths.
One of the fundamentals of 3D programming is Vectors. If have two location vectors A and B,
A vector C describing the shortest distance from A to B
C = B - A

distance from A to B
C.length

there is almost never a need to calculate this using the Euclidean distance formula.
Making a small adjustment to answer to Calculating and exporting global rotation of faces created from splitting an isosphere
Instead of choosing the single face find the poly that matches the criteria of the question.

Average the face centers
Sort by distance from face center to calculated average
Use the closest

Since this is returning a face, there is no way (without weird ngons) this can be &quot;outside the faces&quot;
Note this will favour small tightly clumped faces.  Consider perhaps weighting by the face areas How to compute the centroid of a mesh with triangular faces without using Blender&#39;s built-in functions?
Also a point on another face may be closer to the median of face center medians.  Aside from raycast, Object.closest_point_on_mesh(...) will also return the face.
hit, loc, norm, index = ob.closest_point_on_mesh(o)

if hit:
    f = ob.data.polygons[index]

Test script: Run in object mode, sets origin to each selected object based on criteria above.
import bpy
from mathutils import Quaternion, Vector

context = bpy.context

z = Quaternion()

def closest_poly_to_centers_avg(polys):
    o = sum((p.center for p in polys), Vector()) / len(polys)
    return(sorted(polys, key=lambda p: (p.center - o).length).pop(0))

for ob in context.selected_objects:
    print(ob)
    me = ob.data
    if not me.polygons:
        continue
    mw = ob.matrix_world
    mwi = mw.inverted()
    f = closest_poly_to_centers_avg(me.polygons)
    c = mw @ f.center
    n = mw @ f.normal

    q = z.rotation_difference(n.to_track_quat())
    M =  q.to_matrix().to_4x4()
    M.translation = c
    me.transform(mwi @ M.inverted())
    ob.matrix_world = M

FWIW I  really loathe this
for everyObject in objects:

having a variable named every object that is one of each, may &quot;read well&quot;  but really? Equally with someObject, somePolygon opposed to simply ob and f