Support

Account

Home Forums Add-ons Gallery Field How to extract the serialized array

Solving

How to extract the serialized array

  • Hello guys!

    I’m trying to extract the images to my Gallery in a xml file, outside of wordpress, all this via select, but we know that the gallery field serializes the images in the array in this way, how do I extract these images if the array is serialized, I tried “unserialize” but only returns this:
    Array ([0] = > 2246 [1] = > 2247) 1

  • Are you using get_post_meta() to get the value?

    What do you want to put in the XML file? The URL of the full size image or something else?

  • Thanks for reply @hube2

    No, I’m using select manual because I’m not inside of wordpress so you can use get_post_meta()

    Something like this

    
    //conection... bla bla bla or
    global $wpdb;
    
    $single ="SELECT DISTINCT
    ID AS id,
    post_title AS title,
    (SELECT meta_value FROM ".$wpdb->posts." WHERE post_id = ID AND meta_key = 'images') AS images
    FROM
    ".$wpdb->posts."
    WHERE
    post_status = 'publish' AND
    post_type = 'property'
    ";
    
    foreach($wpdb->get_results($single, OBJECT) as $rss) {
    
      echo $rss->images; // is array serealized, how extract?
    
    }

    I wanted to pull all the images from the Gallery, could be perfectly for full url, Yes

  • If you’re not inside WP then it will be extremely difficult to get what you’re looking for. It’s going to take some additional queries.

    ACF stores only the attachment ID in a serialized array. You will first need to unserialize the array and then you will need to do queries to get the image URL.

    If I was doing this inside of WP I would do something like this.

    
    $image_array = maybe_unserialize(get_post_meta($post_id, 'gallery_field', true));
    $count = count($image_array);
    for ($i=0; $i<$count; $i++) {
      $image = wp_get_attachment_src($image_array[$i], 'full');
      $image_src = $image[0];
    }
    

    I am not familiar with exactly how you’d do that with SQL.

  • If it was inside of wordpress would use their own functions of ACF, but i need to pull these images inside of an xml file to be shown in another location that does not use wordpress.

    @hube2
    I appreciate you have lost time trying to help me, but for now we have no solution:(

  • I was hoping that the information I provided would help you find the information you needed. The database query that you’re doing will not get what you want.

    You’re going to have to do another query of the postmeta table using each of the ID values in the array. I think that you can query the meta_key _wp_attachment_metadata. This will give you another serialized array and this array will contain the urls for each of the different image sizes. Then you’ll need to extract what you want from there.

  • Yes, Something like this

    
    //conection... bla bla bla or
    global $wpdb;
    
    $single ="SELECT DISTINCT
    ID AS id,
    post_title AS title,
    (SELECT meta_value FROM ".$wpdb->posts." WHERE post_id = ID AND meta_key = 'images') AS images
    FROM
    ".$wpdb->posts."
    WHERE
    post_status = 'publish' AND
    post_type = 'property'
    ";
    
    foreach($wpdb->get_results($single, OBJECT) as $rss) {
    
    $img = $rss->images;
    $gallery = unserialize( $img );
    $images = implode(", ", $galeria);
    $photos = "SELECT
    meta_value AS photos
    FROM
    ".$wpdb->postmeta."
    WHERE
    ".$wpdb->postmeta.".meta_key =  '_wp_attached_file' AND
    ".$wpdb->postmeta.".post_id IN($imagens)
    ";
    foreach($wpdb->get_results($photos, OBJECT) as $rss) {
     echo $rss->photos; // 2016/01/photo.jpg
    }
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘How to extract the serialized array’ is closed to new replies.