Ready to get started?Download WordPress


[resolved] Custom Field Data Lost upon trashing? (3 posts)

  1. wpgods
    Posted 2 years ago #

    I've created a custom post type with 2 custom fields attached to it... When I create a post of this type and fill in the custom fields, the data is, in fact, saved to the db... However, if I trash one of them, then that post's custom fields data is deleted from the database.

    I thought trashing was just supposed to basically change the post_status to "trash" so it's out of view unless you actually delete it permanently?

    If that's the case, why am I losing my custom fields data when I trash an item?

  2. wpgods
    Posted 2 years ago #

    Here's all the relevant code for the above question:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    The custom post type doesn't support the "title" portion of posts, so it's not included as one of the columns in the edit posts page. Therefore, I had to hack my own version of the row_actions() function to enable the edit/quick edit/trash/view links when each post is hovered.

    All of the functionality works as it should... Except that the "testimonial author" and "attribution link" values are deleted from the database when moving a post to the trash. No idea why...

  3. wpgods
    Posted 2 years ago #

    It seems that the action "save_post" gets fired when moving a post into the trash... So my save method was being run with no $_POST data to process.

    The solution wound up being to add a nonce to the save process for the custom fields, this way they don't get overwritten when a post is moved to the trash since the nonce is sent with the $_POST data.

    A good idea anyway, not sure why on earth I didn't think of it sooner.

    [Code moderated as per the Forum Rules. Please use the pastebin]

Topic Closed

This topic has been closed to new replies.

About this Topic