Creosote would have been the go to for this, but in the EU (and ongoing, still in the UK, regardless of Brexit) only professionals are licensed to use it because of its carcinogenic properties. There is a kind of replacement called Creocote which does not have the same problems and is more environmentally friendly; it is still oil based, unlike most of the other fence preservatives on sale, and therefore will still have a higher level volatile oil content, meaning it has a strong odour and should be kept off any nearby plants. Some guidance on fence preservatives is given here
https://home-gardener.co.uk/fences/fence-preservative/.
If you are yet to buy fence posts, make sure you choose pressure treated/tanalized ones - these are pre treated for rot. The manner of installation is important too - rotting usually starts at the base, where they are near the soil; metposts can help to avoid this. Otherwise, concrete pots (with a slot to insert fence panels) last a lot longer than wooden ones; use of concrete gravel boards under the panels also means a longer lasting fence.