update_post_meta problem saving field values (5 posts)

  1. r0bbiem
    Posted 3 years ago #

    I have a bunch of custom meta boxes and fields associated with a post type, all created in a plugin file. I can't seem to get the values to save or persist when I hit the update button.

    Here's my code http://pastebin.com/mw389heA

    Hope someone can get me out of the woods with this one, many thanks in advance :]

  2. bcworkz
    Posted 3 years ago #

    You may be getting an ID collision with the last post in the last loop run. You do not need the global $post; line in the function initYoutubeMb() because $post (the one being edited) is passed as a parameter. The global $post is the last post in the last loop, not the one being edited.

    I'm not confident this is the problem, but it's something to address anyway.

  3. r0bbiem
    Posted 3 years ago #

    Thanks bcworkz, that didn't solve the problem but good to know.

    I have a nagging feeling that I'm not defining my $meta_keys (such as 'track_artist') before I try to populate them, but I'm not familiar enough with this process.

    Should I be using an add_post_meta() before I use update_post_meta()?
    Only problem I can see with this is that add_post_meta() requires a value and I would just want to define the keys at first without populating them with values.

  4. bcworkz
    Posted 3 years ago #

    It should not matter, most WP update functions will add if the key does not exist, a quick read of the source of the underlying update_metadata() indicates the same situation here. Since we're just scratching our heads here, wouldn't hurt to try using add_post_meta(). You could always insert a temporary "tbd" value or something. I doubt it'll make any difference though.

    I really don't see any glaring problems with your code, it's pretty straight forward. All I can suggest at this point is to insert var_dump($testvalue); die(); at various points to verify actions are firing and functions are returning expected values. Good luck!

  5. MickR
    Posted 3 years ago #

    Hi rObbiem,

    you might have got past this by now, but had a similar prob when trying out something similar here (update_post_meta not creating a new entry).

    http://codex.wordpress.org/Function_Reference/add_post_meta provided an answer - basically use add and update in the same instruction and if the key doesn't exist it will create one, eg:

    <?php add_post_meta(id, 'key', $data, true) or update_post_meta(id, 'key', $data); ?>

    (maybe try entering null data and see if it solves the prob?)

Topic Closed

This topic has been closed to new replies.

About this Topic