update_post_meta returns true, but doesn't work.
-
Hi all,
Hitting a major brick wall using update_post_meta in a particular instance. I’m writing a plugin and am using the the method in various other parts of the plugin, but for some reason it’s not working in this particular metabox.
Here is the textarea:
<textarea id="sb_tweetbox" name="sb_tweetbox"><?php get_post_meta('_sb_saved_tweet'); ?></textarea>
Here is the instance for calling update_post_meta:
add_action('save_post','twtSavePost'); function twtSavePost(){ global $post; update_post_meta($post->ID,'_sb_saved_tweet',$_POST['sb_tweetbox']); }
I confirmed that the $_POST data was coming through and that $post->ID was correct by using wp_die to output it. I also confirmed that the save action is working this way. When I assign update_post_meta to a variable and output the result it comes back at “1”, which should mean that it successfully added the data to the database. However, this is no the case.
I am using update_post_meta all throughout the plugin and in my theme as well and I can’t seem to find out why this particular instance is “working” but not working. Any assistance would be appreciated.
-
Your call to
get_post_meta()
to populate the textarea is wrong. You need to specify which$post->ID
the metadata is for, and you should set the third argument totrue
to get a single value instead of an array.get_post_meta($post->ID, '_sb_saved_tweet', true);
assuming the post object passed to
the add_meta_box()
callback that generated the textarea HTML is assigned to$post
.I think you will find the values have been saved to the database all along, however the value was not being returned to populate the textarea, so it appeared to not save.
You’re right about get_post_meta. That’s something that would have caused me some grief later, so thanks. However, I’m looking directly in wp_postmeta to see if the data is there using MySQL Workbench. It doesn’t show there and is still missing even when I am correctly using get_post_meta.
On first look, I didn’t see any problems, so I actually inserted your script into a metabox I already have as a test. Only then did I discover the errant function call. However, the data saved just fine. The one thing I changed to work with my metabox was the global $post. Your callback is actually passed both the post ID and the post object as parameters, so there is no reason to use globals.
Try using the ID passed instead of getting it from the global. It’s the right thing to do, but I fear it will not help, as the only thing that could go wrong using the global is the value is assigned to the wrong post. I suspect you would have seen the value if it were there, even if for the wrong post. Unless you checked by querying by post ID?
If using the passed ID does not help, you’ve probably run into some sort of caching error. WP does cache metadata, though it should not affect saving data. Your host may also be caching DB traffic. You could probably rule WP out as the culprit by checking the data before and after the actual insert query. Review the source for
add_metadata()
in wp-includes/meta.php to gain insight about what to hook and what to check for.$meta_type
will be set to'post'
. Good luck.I thought I replied to this, but apparently not. When I checked the DB directly I looked for the meta key, which should have shown whether the post id was correct or not. I threw the
wp_cache_flush()
command to see if that would help, but the problem persists. I’m working on a private server and I don’t believe DB caching is enabled.The thing that’s bothering me the most is that the method is returning true when it’s not writing to the DB. Thanks for your help thus far.
I’m happy to help “thus far”, but I don’t know if I’ll be much more help, I’m pretty much out of ideas. Looking at the source code, I do see how an update can fail and the function still returns true. I guess “true” just means the query ran, not that it succeeded. How odd.
But an interesting clue too. It appears when the data is first added, an “insert_id”, an integer, is returned. The fact you are getting “true” indicates some data already exists somewhere. Not sure this really helps, but I find it interesting.
Any chance another plugin, or even your theme has hooked into the metadata process and is confusing things? Might be worth trying disabling all other plugins to see if that makes a difference.
Yup. It was a plugin. I had an old Soundcloud plugin I was using and once I disabled it, it started working. I looked through the plugin to see what could be the cause of the conflict but found nothing. Very strange. Once again, thanks for you help!
- The topic ‘update_post_meta returns true, but doesn't work.’ is closed to new replies.