Created
October 14, 2024 07:24
-
-
Save kshojib/28acd9a419c6553a2d152f45dc6ce3fa to your computer and use it in GitHub Desktop.
Regenerate thumbnails by meta query
This file contains 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 | |
# Set the admin user with the right permissions (replace with the actual username or user ID) | |
ADMIN_USER="shojib" | |
# Define the brand you want to filter by (adjust the value as needed) | |
BRAND="World Furniture" # You can set your brand name with spaces here | |
# Get all product IDs where the ACF field 'brand' equals the specified brand (handle spaces with quotes) | |
PRODUCT_IDS=$(wp post list --post_type=product --meta_key='brand' --meta_value="$BRAND" --format=ids --user=$ADMIN_USER) | |
# Check if any products are found | |
if [ -z "$PRODUCT_IDS" ]; then | |
echo "No products found for the brand '$BRAND'." | |
exit 1 | |
fi | |
echo "Products found for the brand '$BRAND': $PRODUCT_IDS" | |
# Loop through each product ID | |
for PRODUCT_ID in $PRODUCT_IDS | |
do | |
# Get the featured image ID for each product | |
FEATURED_IMAGE_ID=$(wp post meta get $PRODUCT_ID _thumbnail_id) | |
# Get the gallery image IDs (stored as a comma-separated list in '_product_image_gallery') | |
GALLERY_IMAGE_IDS=$(wp post meta get $PRODUCT_ID _product_image_gallery) | |
# Combine featured image and gallery images into one list of IDs | |
ALL_IMAGE_IDS="$FEATURED_IMAGE_ID $GALLERY_IMAGE_IDS" | |
# If images exist, regenerate them | |
if [ ! -z "$ALL_IMAGE_IDS" ]; then | |
echo "Regenerating images for product ID $PRODUCT_ID..." | |
echo "Images: $ALL_IMAGE_IDS" | |
echo "$ALL_IMAGE_IDS" | tr ',' '\n' | xargs wp media regenerate --yes --user=$ADMIN_USER | |
fi | |
# Wait for 3 seconds before processing the next product | |
# sleep 1 | |
done | |
echo "Regeneration of featured and gallery images complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment