Home Forums Add-ons Gallery Field update_post_meta() and gallery field


update_post_meta() and gallery field

  • In my project I need to post images directly to db, without admin panel. For this purpose I’m uploading images with media_handle_sideload() and then update the post with update_post_meta().

    To update gallery field I’m uploading images and then write its IDs to the string like that a:4:{i:0;s:4:”1174″;i:1;s:4:”1175″;i:2;s:4:”1176″;i:3;s:4:”1177″;}.
    I understand that a:4 – means 4 images, i:0 – index (like in array), but can’t understand what is s:4 about. When I submit a post with GUI in admin panel it can be s:2 or s:3 or s:4. Please help me to figure it out, what is this value about?

  • Hi @semanalitics

    The string you are referring to is a serialized array. You can first create an array of ID’s, and then save the array to the DB via the update_postmeta function will will serialize the array for you!

    You can also make use of the handy update_field function here:


  • Actually, in the first message I wrote that I’m using serialized array a:4:{i:0;s:4:”1174″;i:1;s:4:”1175″;i:2;s:4:”1176″;i:3;s:4:”1177″;}
    What I need is to understand what “s” parameter means! How can I determine its value?

  • If I add images to the gallery field via admin panel (not directly to db), this parameter equals to “2” or “3” or “4” but why precisely?

  • @semanalitics I believe the s:4 is the length of the string value.

    For instance, in my database, I have this:

    s:14:”Walk-in closet”


    s:16:”Washer and dryer”

    Note the values equal how many characters follows.

    I found your thread trying to figure out how to update the values globally (to fix typos) without editing the values and resaving every page that used it. Figured I’d help you while continuing to look for my answer. 🙂


  • It is a string length indeed. Thanks a lot!

  • My pleasure. Now if I could solve my issue. 🙂

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘update_post_meta() and gallery field’ is closed to new replies.