Python function behaves differently in a loop?
The loop is not the problem. The dimensions are based on the selection, which is why running the loop did not work.

All you need is to add in selection and deselection to your script.

Notice on line 6 ob.select = True and on line 25 ob.select = False.
That selects the object the loop is working on, then deselects it when that iteration of the loop is done.
The reason you need to select and deselect each object is because ob.dimensions is the bounding box of the selection, not just one object.
Which is why it seamed to work for only one object at a time.

Entire script:

import bpy
import math

def set_orientation(ob, target_rotation):
    bpy.context.scene.objects.active = ob
    ob.select = True
    #ob: object to operate on

    #this assumes you are starting at 0 rotaton, so you pobably want to apply rotation before doing anything:
    bpy.ops.object.transform_apply(location=False, rotation=True, scale=False) #apply rotation
    #get inverse of rotation we want:
    t_rot = (math.radians(target_rotation[0]*-1),
             math.radians(target_rotation[1]*-1), 
             math.radians(target_rotation[2]*-1))
    #we need to apply the rotations in reverse order, so unsure that we are in zyx:
    ob.rotation_mode = 'ZYX'
    ob.rotation_euler = (t_rot) #set rotation to inverse
    bpy.ops.object.transform_apply(location=False, rotation=True, scale=False) #apply rotation

    t_rot = (math.radians(target_rotation[0]),
             math.radians(target_rotation[1]), 
             math.radians(target_rotation[2]))
    ob.rotation_mode = 'XYZ' #set rotation mode back (was zyx, so we want xyz)
    ob.rotation_euler = (t_rot) #set rotation
    ob.select = False

def fix_orientation(ob):
    if ob.type != 'MESH':
        return

    d = ob.dimensions
    correction = (0,0,0)

    if d[0] &gt; d[1] and d[0] &gt; d[2]:
        correction = (0,0,90)

    if d[1] &gt; d[0] and d[0] &gt; d[2]:
        correction = (0,0,0)

    if d[2] &gt; d[0] and d[2] &gt; d[1]:
        correction = (90,0,0)

    # hack for fixing rotation on boards at 45 degree angles:
    if abs(d[0] - d[1]) &lt; .1:
        correction = (0,0,45)

    if abs(d[0] - d[2]) &lt; .1:
        correction = (0,45,0)

    if abs(d[1] - d[2]) &lt; .1:
        correction = (45,0,0)

    set_orientation(ob, correction)

for ob in bpy.data.objects:
    fix_orientation(ob)