Skip to content

Instantly share code, notes, and snippets.

@dgwyer
Created August 14, 2023 14:41
Show Gist options
  • Save dgwyer/b449066f65cf9de1e1cd561777cf3e72 to your computer and use it in GitHub Desktop.
Save dgwyer/b449066f65cf9de1e1cd561777cf3e72 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Start the timer
start_time=$(date +%s)
# Vars
import_images=12
# 1. IMPORT MEDIA
# Directory path where the images are located
DIR="Path to images"
# Loop through the range of images
for i in $(seq 1 $import_images); do
{
# Construct the image path
IMG_PATH="$DIR\\$i.jpg"
# Import the image using wp media import
ATTACHMENT_ID="$(wp media import "$IMG_PATH" --porcelain --quiet)"
# Create a post
POST_ID=$(wp post create --post_title="Post Title for $ATTACHMENT_ID" --post_status=publish --post_type=post --porcelain --quiet)
wp post meta set $POST_ID _thumbnail_id $ATTACHMENT_ID --quiet
# Create a page
PAGE_ID=$(wp post create --post_title="Page Title for $ATTACHMENT_ID" --post_status=publish --post_type=page --porcelain --quiet)
wp post meta set $PAGE_ID _thumbnail_id $ATTACHMENT_ID --quiet
# Create a WooCommerce product
PRODUCT_ID=$(wp post create --post_title="Product Title for $ATTACHMENT_ID" --post_status=publish --post_type=product --porcelain --quiet)
wp post meta set $PRODUCT_ID _thumbnail_id $ATTACHMENT_ID --quiet
echo "($i/$import_images) Successfully imported image $i.jpg and set attachment ID to $ATTACHMENT_ID. Create post, page, and product with featured image."
} &
done
wait
# End the timer
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
# Calculate minutes and seconds
minutes=$((elapsed_time / 60))
seconds=$((elapsed_time % 60))
# Display completion message with formatted time
echo "Completed in ${minutes}m ${seconds}s"
@dgwyer
Copy link
Author

dgwyer commented Aug 14, 2023

This is a simplified version of the full script I'm using to seed a WordPress db prior to CI/CD testing. It parallelises the post creation loop significantly improving processing time.

Ref. https://twitter.com/dgwyer/status/1691083133985161216

@nhogan411gce
Copy link

@dgwyer appreciate you sharing this.

You mentioned here that you were adding "sample post content via Lorem Ipsum API call". I've not seen that done before and particularly interested in seeing how you do that. Any change you could provide sample of that code as well.

Again, really appreciate the share. Will let you know if a can bring the time down any further.

@dgwyer
Copy link
Author

dgwyer commented Aug 14, 2023

Sure. I just shared how to do this here in a Gutenberg 'block friendly' way.

https://twitter.com/dgwyer/status/1691125746067034116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment