WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Update post meta function is deleting post meta data. (6 posts)

  1. giant slayer
    Member
    Posted 2 years ago #

    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?

    Thanks,
    christopher

  2. giant slayer
    Member
    Posted 2 years ago #

    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.

    Thanks!!
    --christopher

  3. giant slayer
    Member
    Posted 2 years ago #

    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.

    Thanks,
    christopher

  4. giant slayer
    Member
    Posted 2 years ago #

    When I try to include the $prev_value parameter in the update_post_meta function, the meta data still disappears.

    I'll keep trying new things.
    Thanks,
    christopher

  5. giant slayer
    Member
    Posted 2 years ago #

    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: O:16:"SimpleXMLElement":1:{i:0;s:14:"rice-paper.jpg";}
    When I echo the value in my variable just before updating the meta, the value printed is rice-paper.jpg.

    Why does update_post_meta not put in the value rice-paper.jpg instead of putting in the other?

    Thanks,
    christopher

  6. giant slayer
    Member
    Posted 2 years ago #

    I figured it out.
    This is what I had to do.
    I had to use the sprintf function.
    sprintf("%s", $freebackgrounds->background[$i]->imagefilename)
    This converted the xml data into a string format would could be a string value stored in a variable.

Topic Closed

This topic has been closed to new replies.

About this Topic