The Problem with Previewing Posts


There’s a bit of a problem with previewing posts in WordPress. When a post has metadata associated, let’s say you’re using the excellent Advanced Custom Fields plugin, and you hit the preview button – you can see content changes but none of the custom fields appear to be set in the preview. Odd.

But then you take a look at the live post. The custom fields have been updated there instead of on the preview!

This is because of the way previews work, internally WordPress creates a revision and displays that. The problem is the that the form to save the post is still submitted, the standard ‘save_post’ hook is fired and all those well intentioned plugins and themes dutifully update the post meta.

There has been some work under way on this trac ticket #20299 however there’s not been a consensus on how to do this in a generic way. The long term solution is a full blown post meta revision system like the one Alex King is already using (trac ticket #20564). For some reason only post format metadata gets revisioned. Seems strange to me that a generic solution could not be implemented.

Anyway, enough moaning. Here’s a code snippet you can use in your plugins or themes to get around the problem with previews for the time being:

One response to “The Problem with Previewing Posts

  1. I’ve noticed this doesn’t seem to work for me – I’m guessing because this post is a couple of years old. Anyone have any luck making it work in the latest version of wordpress?

    (It appears to just strip the preview of any metadata at all.)

    Doesn’t look like we’re much closer to having a bug fix in the core WordPress, far as I can see!

Leave a Reply to Yaz Cancel reply

Your email address will not be published. Required fields are marked *