Forums

Custom Field Data Being Cleared (6 posts)

  1. brenton.crowley
    Member
    Posted 8 months ago #

    Hi,

    This is my fist post and it is the first time I have investigated wordpress and actually PHP as well so there is probably a variety of things that I am probably overlooking.

    Basically I've created some custom fields in a meta-box for a custom type. It all works great except for one thing. When I publish the data and then view the custom post the data displays but if I leave the 'edit post' screen open for say around a minute it appears to clear all the data that was stored in the custom fields. So if I refresh the post all the custom field data is wiped, left with nothing.

    The problem only appears to happen when I leave the 'edit post' screen open. I'm not sure if it makes a request each minute or if it wipes it. I was hoping that someone may be able to shed some light on the topic. If I close the 'edit post' screen the actual post is fine and the data is always displayed.

    The strange thing is any content that is in the WYSWIG box remains in tact so I guess I am probably doing something incorrect somewhere.

    I followed this tutorial so my code is pretty similar to what is on here: http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/

    If you would like me to post actual code I can but hopefully it isn't necessary since i hope it's something that hasn't been brought to my attention.

    cheers,
    Brenton

  2. esmi
    Theme Diva & Forum Moderator
    Posted 8 months ago #

    Could these "wipes" be when WP auto-saves a post revision?

  3. brenton.crowley
    Member
    Posted 8 months ago #

    Yeah I was thinking it might be something like that. So it doesn't take into account the current custom field data. It is strange that it wipes it though even though there is some existing data in there.

    Is there a way to append the meta-data to an autosave? Strange that it also affects the published post.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 8 months ago #

    I've just tried replicating your problem with one of my themes that has custom meta boxes but, despite leaving the edit post open for a good 20mins, I couldn't freshly entered custom meta content to disappear. So it could be something to do with the way in which you have constructed those meta boxes.

    Have you reviewed http://codex.wordpress.org/Function_Reference/add_meta_box I think I relied pretty heavily on that documentation when I built my meta boxes.

  5. brenton.crowley
    Member
    Posted 8 months ago #

    Ok mate I will give it a read through over the weekend. Thanks for your help at least I know the problem lies somewhere in my code and not to do with WordPress. I'll let you know my result in the next few days :)

    cheers

  6. brenton.crowley
    Member
    Posted 8 months ago #

    Ok so solution lied within the autosave. Custom fields aren't saved in the autosave method so you need to check whether there is an autosave taking place.

    This worked fine for me in my save_post method callback:

    global $post; 
    
    	$post_id = $post->ID; 
    
    	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    	return $post_id;

    cheers and thanks for the insight!

Reply

You must log in to post.

About this Topic