I'm setting up a real estate website. I make listings using a custom post type named listings in which I have multiples meta boxes for beds, baths, and various other information.

I use a plugin to import these posts because listings are generated from third-party software (XML file with Xpaths). Everything works fine when importing listings.

The BUT is that the theme inserts images attached to the post _wp_attach_file ... file.

The XML makes easy to get the paths to images, but I can't figured out how to make them 'be attached' to the post to load up.

Is it possible to attach them using custom fields?

(I need to do this for almost 500 posts through WP ALL IMPORT - XML XPath importer plugin - which I can insert custom_fields and meta boxes lines.)


I would recommend just modifying the single-listings.php template file to output the gallery directly:

<?php echo do_shortcode( '[gallery] ); ?>

That way, for all the images that have been imported and attached to a given CPT, they weill be output just as if you had placed a [gallery] shortcode in the post_content for each post.


If you have post metadata that contains image URL paths, and you want to upload/attach those images to the post, then have a look at media_sideload_image() (or, in 3.5, wp_sideload_image()).

  • I does that already, the problem is that there is no attached files the CPT. I'm trying to figure out how to attach them. I tried to set the path to the file with custom field for each post: _wp_attached_file -> DIR/uploads/2012/IMAGE.JPG and i doesn't work, any idea of what i'm doing wrong? – Furtado Nov 20 '12 at 20:05
  • Okay, I was confused by the wording of the question. So you don't actually have images attached to the post, but rather you have custom post metadata for the images, with meta keys like _wp_attach_file? – Chip Bennett Nov 20 '12 at 20:12
  • I manually upldd images through ftp (16000 images), those imgs are the listings imgs (500 listings). The theme, only calls images when they are attached to the CPT. I cant manage to add them manually (post by post), so I have to find a way to attach them when I do the import. Importing the listings I can set custom fields for each, using the XML to provide the path. I browsed up the db to see an example of an attached file looks like and it shows a meta_key and meta_value (key-> wp_attached_file | value-> 2012/11/IMAGE.JPG). So I am trying to _attach it like that. but it isnt working. – Furtado Nov 20 '12 at 20:30
  • I don't know if the meta_id or post_id has something to do with that, I really don't understand much about it. Is those path's case sensitive? (thanks for your attention) – Furtado Nov 20 '12 at 20:32
  • 1
    I'm so stupid. I didn't noticed there was a field saying: ATTACHMENTS on the import wizard... Solved A BIG THANKS – Furtado Nov 20 '12 at 20:59

Furtado figured out the answer and posted it as a comment buried under the original post. WP All Import has an option to import images as attachments:

I didn't noticed there was a field saying: ATTACHMENTS on the import wizard... Solved A BIG THANKS

  • Re-posted this answer from a comment for visibility. – Dave Romsey Nov 30 '16 at 9:17

If your uploading the photos via ftp, WordPress won't know about them in the media library. If that's Ok with you then you take the image URLs from the XML being loaded, create a serialized array using maybe_serialize() and then save it as a custom field to the post.

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.