I am trying to update a custom meta variable using the update_post_meta function. I have the following code written.
update_post_meta(1054, 'freeimage_filename', $freebackgrounds->background[$i]->imagefilename);
Everytime I run this, the data is deleted and the page no longer shows the variable.
I have checked that the variable has a value and echo it in the line prior and it has a valid string.
Any ideas why the new value is not replacing the existing value and why the existing value is deleted?
I can use the get_post_meta function to retrieve custom meta data I manually enter into my page. However the update_post_meta function only deletes the data stored in the custom meta variable. It never retreives the data. I have tried it with several different variable names and the deleting data behavior is the same.
Also I am trying to get post meta from a page rather than a post if that makes any difference.
I have tried a few different things.
I tried to manually delete the value of the custom meta variable from the page screen and then use the add_post_meta function to add it instead of trying to update. No post_meta variable name or value was added.
I also tried to use the delete_post_meta function followed by the add_post_meta function. Again, nothing was added.
Interestingly, I am using the update_post_meta function only two lines later to update a custom date field and it works fine. Both updates are to custom post meta on the same page. One works, the other does not.
When I try to include the
$prev_valueparameter in the update_post_meta function, the meta data still disappears.
I’ll keep trying new things.
I looked in my database at the wp_postmeta table and saw several entries from page ‘1054’ and meta_key ‘freeimage_filename’
The stored meta value is as follows:
When I echo the value in my variable just before updating the meta, the value printed is
Why does update_post_meta not put in the value
rice-paper.jpginstead of putting in the other?
- The topic ‘Update post meta function is deleting post meta data.’ is closed to new replies.