WP All Import - Importing images into a post specific directory ie Property Listing folder

486 views Asked by At

WP All Import - Importing images into a post specific directory ie Property Listing folder

Case We have a setup to sync MLS listings into Wordpress with images, we have maybe 30-50 images per listings with ~500 listings per day being updated.

We then delete listings and images as they go off market. Currently it takes a fair bit of processing power to lookup the listings media, delete the related media and then delete the posts as they date (say 100 listings are aged per day ~5000 images we need to hunt and delete.)

Secondly we have an issue with updating images and keeping the image path as the listing updates on a regular cycle and these media paths are then synced with other systems. Having a logical directory path will make it significantly easier to stablise the logical url path and bulk delete the media off the server via CLI once when the listing is removed from the DB

So in an ideal world we would like to grab a custom field (the MLS number which is the listing unique id). Create a directory in the uploads folder Then any media that is uploaded and associated with a Post/listing, we would then upload the media into this folder.

What is the most elegant and desired method to

  1. Create a post specific directory folder if it doesnt exist
  2. Upload any media uploaded via WP All Import to be uploaded to this new specific folder
  3. Make sure that the resizing and all the optimised tools know the relative location of each asset and the resized assets to be contained within each relevant directory

We had this logic with WP Property importer and it worked well. https://wp-property.github.io/addons/importer/#gsc.tab=0

1

There are 1 answers

0
Torn Marketing On

For those playing at home

In this example you create the post first then run another import for the images which then uploads the images to the now created post_id folder within the custom folder.

You want to make sure you have the listing as a draft if no images and only update records not create from the images.

function wpai_set_custom_upload_folder($uploads, $articleData, $current_xml_node, $import_id) { 
    // Change our upload path to uploads/customfolder.
    $uploads['path'] = $uploads['basedir'] . '/listing/' . $articleData['ID'];
    $uploads['url'] = $uploads['baseurl'] . '/listing/' . $articleData['ID'];
    
    if (!file_exists($uploads['path'])) {
        mkdir($uploads['path'], 0755, true);
    }
    return $uploads;
}

add_filter('wp_all_import_images_uploads_dir', 'wpai_set_custom_upload_folder', 10, 4);