Custom Fields
Re-Envisioned
Support General Manually/Programmatically update Image Advanced field values

  • Creator
    Topic
  • #18026
    Resolved Mohammad Zahed Kamal
    Participant

    Hi, I’ve checked the documentation. But couldn’t find how can i updated/delete images from the Image Advanced field. Please help

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #18034
    Anh Tran
    Keymaster

    Hi Mohammad,

    You can do that with this code:

    // Add
    $your_images_ids = [1,2,3];
    foreach ( $your_images_ids as $image_id ) {
        add_post_meta( $post_id, $field_id, $image_id, false ); // Notice the last parameter.
    }
    
    // Delete
    delete_post_meta( $post_id, $field_id, $image_id );
    #18037

    HI, thanks for the response. But above methods gonna create new rows instead of replacing the old values (image IDs) by new values (image IDs) IF values (image IDs) arent same. So, is there any way to avoid deleting any old image IDs that exist in new image IDs

    Thanks

    #18046
    Anh Tran
    Keymaster

    Hi Mohammad,

    For image fields, each image ID is stored in a single row in the DB. If you don’t want to delete old rows, then you can get them all via get_post_meta function and do some check before updating. But I guess it takes more (maybe a lot) of code to check. A better and simpler way to do that is delete all old rows and add new rows.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.