2

i use the code explained here to get id of attached images to a post in a custom post type from $value that contains url of image (images already uploaded & exist in Media Library). the post contains some data and images that users submitted & uploaded via a form. when i publish post for the first time from quick edit menu, that function works perfectly and retrieve id of all attached images then i can use update_field command and add images to acf image field with array of images attachment ids.the problem appears when i update/publish post from Edit Page (not quick edit menu). in this case that function only return id of first attached image and return 0 for other attached images. then acf image field only show that image only.

other Description that maybe useful:

i have a custom field named 'pics' that contains url of uploaded images via form and its value is something like this:

pic03.png
http://insell.ir/wp-content/uploads/fsqm-files/pic03.png

pic02.png
http://insell.ir/wp-content/uploads/fsqm-files/pic02.png

pic01.jpg
http://insell.ir/wp-content/uploads/fsqm-files/pic01.jpg

i use this code in my template to extract urls from 'pics' custom field and save them in $attach_ids as array and finally update acf image field with that:

<?php

$img = get_post_meta( get_the_ID(), 'pics', true);
$imgList = explode("\n", $img);
$attach_ids = array();
foreach ( $imgList as &$value ) {
    if ( strpos( $value, 'http:' ) !== false ) {
        $attachment_id = get_attachment_id( $value );
        array_push($attach_ids, $attachment_id);
    }
}
// print_r ($attach_ids);
$images = update_field( 'slider_portfolio', $attach_ids, get_the_ID() ); 
$images = get_field('slider_portfolio');

?>

in case 1 (publish post via quick edit), print_r ($attach_ids); show correct output that contains all images id:

Array ( [0] => 1276 [1] => 1278 [2] => 1277 )

in case 2 (publish/update post via edit page), print_r ($attach_ids); show this output:

Array ( [0] => 0 [1] => 0 [2] => 1277 )

Any help would be really appreciated. thank you very much.

1 Answer 1

0

after 3 days i found the problem was because of an space in end of urls in $imgList array (Except last url) !!! so i added trim for each url ($value) and solved problem ...

<?php

$img = get_post_meta( get_the_ID(), 'pics', true);
$imgList = explode("\n", $img);
$attach_ids = array();

foreach ( $imgList as &$value ) {
    if ( strpos( $value, 'http:' ) !== false ) {
        $value = trim($value);
        $attachment_id = get_attachment_id( $value );
        array_push($attach_ids, $attachment_id);
    }
}

$images = update_field( 'slider_portfolio', $attach_ids, get_the_ID() ); 
$images = get_field('slider_portfolio');

?>

there isn't any problem with get_attachment_id() function. it's perfect !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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