Say that you have a cuboid "planet" that can retain its shape. Can it have a cubical iron core (as opposed to the spherical one described in this question) and generate a magnetic field? Would the shape of the core affect the shape of the field? Does the fact that the core isn't spherical even make a difference?
A small cube can have magnetic poles. A planet-sized cube cannot.
A cube shape neodymium magnet, its top painted by a black marker. I approach it with some other magnets and — click — they snap together, with the lone magnet having flipped around to align its poles with the others'.
But no, you cannot have a cube shaped world with a magnetic field since a planet's magnetic field is not powered by permanent magnets but by the flow of metallic matter in its inner structure. This requires the inner parts of this thing to be liquid. Liquid — under its own gravity — will never shape into a cube.
Sure, toss a cubical bar magnet into space and you have a "planet" with a magnetic field.
The only question is how big it is. If it's big enough it'll stop being magnetic or cubical.
A spinning core requires a mantle. So if you want to generate the magnetic field that way you need to be big.
Now sure you could build eight three sided mountains on a sphere to form a cube but once it's big enough you need to make the mountains out of unobtainium.
How it formed will require a hell of a back story.
So after reading the comments and other answers lets assume the planet itself is forcing the shape of the core to be cubic. Assuming the pressure on the core is enough to keep it contained and molten forever I (keep in mind that I am not a scientist, I'm just throwing out an idea here) think it would be possible to have a magnetic field, BUT I think it would still be spherical. The reason is as follows:
And the corners that are not within the sphere would not "spin" they would have movement but probably not enough to generate a magnet "pole" that would breach the planets surface. Even if your perfectly cubic iron core made a motion somewhat like this:
I do not think you would get a cubic magnetic field, if anything it might be a monopole field with either the + or - on the outside of the planet and the opposite charge in the center of the core. Again this is just my thoughts on what might happen, i have a limited understanding of how these types of things work but I thought i would give it a go.