1

I have an error in the metadata for a post. It's preventing me from correcting or adding in a new value from the post itself.

If I go into the database postmeta table and go to the entry for that post ID I can see the error.

Can I safely just delete that entire entry in the postmeta table? There's no other post meta to worry about.

Alternatively, I see the WordPress function delete_post_meta(). However, I'm not sure how to correctly use this in functions.php and hook-wise.

I could just delete and re-enter the entire post from the dashboard but I think the entry in the postmeta table for the old post would still be there, orphaned - right?)

1 Answer 1

2

It's unclear what the postmeta actually does so it could effect your website in any number of ways.
At best it will just clear the field / option and the user will need to re-enter it for that specific page.
At worst it could change a pages layout or orphan a media file in your media library ( assuming the field holds an attachment ID ). Other than that I cannot see a reason why deleting a row in the postmeta table would be extremely harmful.

Whenever a post is deleted WordPress does delete all the postmeta assigned to that post ID. The wp_delete_post() function says...

When the post and page [gets deleted], everything that is tied to it is deleted also. This includes comments, post meta fields, and relationships between the post and taxonomy terms.

The delete_post_meta() function can be ran almost anywhere core has been loaded. You could put it inside an init or save_post hook and run it once then remove the function call. For example:

function delete_somemeta() {
    // This will run every single time a page is loaded.
    delete_post_meta( $POST_ID, 'META_KEY_NAME' );
}

You could add this to your functions.php, reload the front-end of your website, then remove it and it will effectively do the same thing as removing the row from the database.

This type of function is most commonly seen as conditional removal of postmeta in save_post hook though when saving metabox data.

2
  • 1
    Thank you for the help on this. Interestingly, the table for posts doesn't include metadata (I think). The entries in the metadata table include meta id, post id, meta key, and the meta value, itself. So, it would appear that, in calling for post data to display, WordPress looks into the metadata table to see if there's any meta associated with the post id. To fix, instead of deleting the metadata table entry, I just removed the error statement in the meta value field. All seems to work, now. Commented Sep 7, 2016 at 17:25
  • Correct, all of WordPress postmeta is in the postmeta table which it does a MySQL JOIN on the post_id to grab any related postmeta to said post.
    – Howdy_McGee
    Commented Sep 7, 2016 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.