Want to install an outdoor ceiling fan directly to exposed 2x4 on a covered porch
You want the main attachment for the fan box to be attached perpendicular to the floor, not horizontally into a faming member. The twist on a horizontal attachment could lead to failure. The box should be attached with sizable screws or lag bolts.

That means, you should have a framing member directly above the box, either the 2x4 you mentioned or a short added framing piece running perpendicular (vertically) to the joist.

I would build a small wooden frame around the fan box. 2x or 1x pieces should do. They are not structural but decorative and gap filling. These will give a base larger than the canopy.

I would then run a conduit to carry the cable to the fan box, making holes in the surrounding box trim.