Home › Forums › Bug Reports › Meta is being duplicated in database
On save of post, the updated ACF field creates another meta row in the database. Image attached. Also weird to note, the post ID is somehow being assigned a random incremented number.
WordPress v.4.2.4, ACF v.5.2.8
This is normal. The second entry in the meta table starts with an underscore with a value of the fields key. This is how ACF knows what kind of field it is and what value to return for functions like the_field()
and get_field()
. The post_id value is not random, that’s the post ID. Everything is a post, if you upload images or files while working on a post, those get the IDs between the post IDs that you see. There are a lot of things that go into the post table, custom post types, menu items, attachments, they all get a post_id.
I understand about the second underscored entry and its function, what I dont understand is why theres 6 entries, there is only one post in this image with no attachments, the only other data being 1 text acf field – so why the duplication of rows, why the different post id’s and why the different meta ids?
Each is for a different post, these posts could be revisions of your one post, they could also be auto saves from your one post. Did you investigate the posts table to see what those posts_id values belong to?
As far as the meta_id value, that is an auto incremented field, every row in the meta table has a unique ID.
Doh! I didn’t realize that these were revisions, I apologize for that John! That explains a bit. However, there is an issue relating to this in another install, see attachment below.
There exists a meta field from ACF ‘micro_class_brians_favorite’ and a custom meta field from another plugin ‘wpcf-micro-class-brians-favorite’. However theres two entries for the acf field, one value being ‘0’ and the other being ‘1’. Currently the backend has it set to ‘1’, so why is the ‘0’ still apparent ( revision perhaps ), but its pulling the wrong value in the front end. This is across all acf fields on this site, as I had to manually delete a field or two in another row for some other acf meta to display in the front end.
This I’m not too sure about.
The field wpcf-micro-class-brians-favorite
shouldn’t have any effect on anything.
I’m just guessing, but when it comes to this field micro_class_brians_favorite
, there are 2 of them. With a standard WP field, this is a valid way to store multiple values to a single field, but that’s not the case in ACF. One of these values is interfering with the other. I’m guessing that there is some minor difference in the way that ACF gets the value on the front end versus the way it gets the value on the back end. But like I said, that’s just a guess.
I can’t say why this field got duplicated this way, was this field managed by a different plugin or method in the past?
At any rate, you may need to go through and manually delete the duplicates for each post_id to correct the problem on the site.
Yeah its super weird. But no, the field micro_class_brians_favorite
was newly created by ACF. Darn..
What type of field is this? Do you see this with more than one field type?
That field is a true/false. Yes i see it with other fields as well, text, textarea, etc.
I can’t say when the duplicate meta table rows where created or what caused them.
I’ve been looking into how ACF gets and updates meta values. It uses the form
get_post_meta($post_id, $meta_key, false);
This means it is getting both values. I have been unable to figure out why it chooses the second value or the first value inconsistently.
When it updates the value it includes the previous value, it doesn’t exactly use this function but what it does is the equivalent
update_post_meta($post_id, $meta_key, $meta_value, $prev_value);
Which is why one value is updated and the other value is not, I think.
Wish I could be more help. However it happened it looks like I was correct. The only way to correct the problem will be to go through the database and manually delete the duplicate meta_key
rows for each post_id
where they are causing a problem.
Something that I didn’t think about earlier is that you may also have a plugin conflict of a filter that’s causing the field duplication. Have you tried disabling all the other plugins on the site and/or switching to the default theme?
Yep, disabled everything, default theme, clean install. Same prob.
If you’ve got nothing but ACF installed on a default theme and you’re still getting duplicate fields. I don’t have a clue. The current version of the plugin does not have a bug that would cause this to happen and I’m not seeing it on any of my sites. You might want to try reinstalling ACF. You might also want to create a support ticket here http://support.advancedcustomfields.com/new-ticket/
For sure, I’ll see if reinstalling it makes a difference, if I can’t figure it out I’ll go ahead and open a ticket – thanks again John!
If I understand you correctly, to get the image you would do something like below. Image and file fields only store attachment IDs
$attachment_id = get_post_meta($post_id, 'image_field', true);
if ($attachement_id) {
$image = wp_get_attachment_image_src( $attachment_id, $size);
if ($image) {
// output image here
// see http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
}
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.