Question

I have this section of code but for some reason the comma isn't being added to separate the array elements. What am I overlooking?

    $imgid = array();

    foreach ( $attachments as $attachment ) {
        $imgid[] = $attachment->ID;
  //    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
    }

    $string = implode(',', $imgid);

    echo $string;

The above foreach code is located in a larger foreach statement:

foreach ($fileslist as $file) {

    if( $file === '.' || $file === '..' ) {
        continue;
    }

    $file = file_get_contents( $post_dir . "/" . $file);

    $filetype = "image/jpeg";

    $filename = "remote_filename_". $i .".jpg";

    $i++;


    xmlrpc_set_type($file,'base64'); // <-- required!
    $params = array($blogid,$username,$password,
                array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
    $request = xmlrpc_encode_request('wp.uploadFile',$params);

    $result = go($request,$rpcurl);
    print_r($result);

    $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => 1,
        'post_status' => null,
        'post_mime_type' => 'image'
    ));

    $imgid = array();

    foreach ( $attachments as $attachment ) {
        $imgid[] = $attachment->ID;
  //    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
    }


    $comma_separated = implode(",", $imgid);

    echo $comma_separated;

}

Basically, what this does is it takes each image in a directory and uploads it to WordPress. There is an attachment ID for each image and I am needing to get each attachment ID saved as a string with each ID separated by a comma.

Edit: Simon Robb provided the correct answer for me. Here is updated and final code:

foreach ($fileslist as $file) {

    if( $file === '.' || $file === '..' ) {
        continue;
    }

    $file = file_get_contents( $post_dir . "/" . $file);

    $filetype = "image/jpeg";

    $filename = "remote_filename_". $i .".jpg";

    $i++;


    xmlrpc_set_type($file,'base64'); // <-- required!
    $params = array($blogid,$username,$password,
                array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
    $request = xmlrpc_encode_request('wp.uploadFile',$params);

    $result = go($request,$rpcurl);
    print_r($result);

    $attachments = get_posts( array(
        'post_type' => 'attachment',
        // 'posts_per_page' => 1,
        'post_status' => null,
        'post_mime_type' => 'image'
    ));

    $imgid = array();

    foreach ( $attachments as $attachment ) {
         $imgid[] = $attachment->ID;
  //    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
    }


    echo "<br />";
    $comma_separated = implode(",", $imgid);

}

echo $comma_separated;
Was it helpful?

Solution

I'm not 100% clear on how your code is intended to work, but currently each $file in your $filelists loop is only returning one attachment, because of the 'posts_per_page' => 1 parameter in get_posts. Therefore the $imgid variable only ever has one element, so implode doesn't have two element to put the comma between.

The output you're seeing is actually one ID being outputted for each loop of $filelist.

If you remove the 'posts_per_page' => 1 parameter, you'll have the full array of attachments in $attachments and it should work fine.

OTHER TIPS

Your logic is sound & should work as expected. But without seeing your larger codebase it is hard to tell what is happening. My only guess is there is a larger loop in play & your initing of $imgid is reseting the array each iteration:

$imgid = array();

Is it possible for you to place that array init higher up in your logic? If so, try that to see what happens. And maybe assiging the index key with the $attachment->ID would help:

$imgid = array();

foreach ( $attachments as $attachment ) {
    $imgid[$attachment->ID] = $attachment->ID;
//    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top