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:

4 responses to “The Problem with Previewing Posts

  1. Hi, I have a similar problem. When save the post, wordpress create 2 revisions but only one with correct metainformation.

    I remove one default hook, for fix the problem:

    In theme file functions.php i put:

    /* Fix revision without meta bug */
    remove_action( ‘post_updated’, ‘wp_save_post_revision’);

  2. But where’s the code!?

    1. Apologies — Looks like the gist has been lost. It’s an old post and unfortunately we tend to be too busy with client work to be able to edit and review old posts.

  3. 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

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