0

I have tried to update a gallery field and the images shows up in frontend but not the backend.

// I have also tried to use the ACF field name like $field = 'field_xxxxxxxxxxxxx';

$field = 'images';

$post_id = 12345;

$attachments_ids = [
    0 => 22222,
    1 => 33333,
    2 => 44444,
    3 => 55555
];

update_field($field, $attachment_ids, $post_id);
0

I'm assuming that $attachments_ids are the new values you want to save.

$field = get_field('images');

$attachments_ids = [
    0 => 22222,
    1 => 33333,
    2 => 44444,
    3 => 55555
];

update_field($field, $attachment_ids, $post_id);
  • In my case $field would be empty. I have created the ACF field in the backend though. – Cudos Oct 18 '18 at 15:08
  • Are you using PHP 7? I see you are using square brackets in the array. – David Walz Oct 18 '18 at 15:14
  • Yes, I am using PHP 7. – Cudos Oct 18 '18 at 15:33
0

I ran into this issue also. This worked for me:

  1. It only seems to work if using the field_xxxx syntax for the selector.
  2. The array of attachment IDs should be flat, with no index key.
    $field = 'field_xxxxxxxxxxxxx';

    $post_id = 12345;

    $attachments_ids = [
        22222,
        33333,
        44444,
        55555
    ];

    update_field($field, $attachment_ids, $post_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.