Support

Account

Home Forums Backend Issues (wp-admin) Only save field in wp_postmeta when data is entered

Helping

Only save field in wp_postmeta when data is entered

  • I was wondering if there is a way to only save a custom field if data is entreated into the field.

    I have a field group of about 10 fields for my posts and not every post is going to be using them, on the posts that I don’t enter in any of the data into the field the post meta data is still saved to the database just with empty meta values. I’m going to have a couple thousand posts so this would give me a lot of unneeded rows in my wp_postmeta table so I was wondering if there is a way to only create a row in the wp_postmeta for a field when data is actually entered into the field to keep the database a bit smaller.

  • This could be done, but not without a bit of work. ACF saves every value, empty or not.

    As far as a solution, I have not tested what follows so it’s only a thought about how it could be done. I have several sites that have thousands of posts in custom post type and usually much more than 10 fields. The empty rows don’t appear to effect anything as far as I can see so I don’t worry about the.

    Anyway, you could create an acf/save_post action with a priority of 1. This will run before ACF saves the data. In your action function you could then delete/unset the $_POST indexes that have empty values. I think this would cause ACF to not save the fields to the database.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Only save field in wp_postmeta when data is entered’ is closed to new replies.