Created
August 14, 2023 14:41
-
-
Save dgwyer/b449066f65cf9de1e1cd561777cf3e72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 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.
Sure. I just shared how to do this here in a Gutenberg 'block friendly' way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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