Manually/Programmatically update Image Advanced field values

Support General Manually/Programmatically update Image Advanced field valuesResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #18026

    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

    #18034
    Anh TranAnh 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 TranAnh 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 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.