Support » Plugins » Hacks » update_post_meta returns true, but doesn't work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    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 to true 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.

    Thread Starter c0nc3pt.SF

    (@c0nc3ptsf)

    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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter c0nc3pt.SF

    (@c0nc3ptsf)

    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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter c0nc3pt.SF

    (@c0nc3ptsf)

    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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘update_post_meta returns true, but doesn't work.’ is closed to new replies.