0

I am getting this ouput when I export the custom field image,

s:267:"a:9:{s:10:"sm-field-0";s:4:"1228";s:10:"sm-field-1";s:4:"1229";s:10:"sm-field-2";s:4:"1230";s:10:"sm-field-3";s:4:"1231";s:10:"sm-field-4";s:4:"1232";s:10:"sm-field-5";s:4:"1233";s:10:"sm-field-6";s:4:"1234";s:10:"sm-field-7";s:4:"1235";s:10:"sm-field-8";s:4:"1236";}";

I can add a function or php to covert this to URLS? but I have no idea. Can anyone help?

0

That is a serialized string, actually it's a serialized string of a serialized string, which is very odd. You need to convert it to an array and then get the image URLs from the IDs that it is returning.

Lets say $str is the variable that contains that string, here's how you would get the image URLs from those attachment IDs. FYI I put the second unserialize call inside an if statement so it won't break if your value ever becomes just a single serialized string

function parseUrlsFromSerializedIds($str) {
    $arr = unserialize($str);
    if (is_serialized($arr)) {
        $arr = unserialize($arr);
    }
    $results = [];
    foreach ($arr as $imageID) {
        $url = wp_get_attachment_url($imageID);
        $results[] = $url;
    }
    return implode(',', $results);

}

Inside that foreach you'll want to do something with the URL. I'm not sure what format you're trying to return them all in so I left it alone.

  • Hi thanks, but it is only returning one image instead, there can be mulipe images per car. I am creating a csv file to grab export images for a car dealership, I need the images url to output all images per car item and comma seperated. link Hopefully you can help more :) – Alex Trainor Feb 18 '19 at 19:32
  • that's the " I'm not sure what format you're trying to return them all in so I left it alone" part. I'll update the answer to get you the urls as a comma separated string – mrben522 Feb 18 '19 at 19:41
  • Your brilliant. Thankyou very much – Alex Trainor Feb 18 '19 at 21:05

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