Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch Create WordPress Posts via wp-cli
###
# Creates 30 dummy posts with featured images via placeimg.com
###
POST_TYPE_SLUG=post;
POST_TYPE_SINGULAR_NAME=Post;
for i in {1..31}
do
echo "Creating $POST_TYPE_SINGULAR_NAME i = [$i]";
post_id=$(wp post create --post_name="Entrant $i" --post_title="Entry $i" --post_type="$POST_TYPE_SLUG" --post_status=publish --post_author=1 --post_date="1987-10-$i 00:00:00" --porcelain --allow-root);
echo "post_id = [$post_id]";
# Download and attach a featured image to the this Bike
wp media import https://placeimg.com/640/480/people.jpeg --post_id=$post_id --featured_image --caption="Image Caption for $POST_TYPE_SINGULAR_NAME $i" --desc="Description for $POST_TYPE_SINGULAR_NAME $i" --allow-root;
# Update the post content with some random stuff
wp post update $post_id --post_content="$(curl https://loripsum.net/api/5/verylong/decorate/link/ul/ol/dl/bq/code/headers)" --allow-root;
# Update the excerpt for this item
wp post update $post_id --post_excerpt="$(curl https://loripsum.net/api/1/short/decorate/)" --allow-root;
# Give the Bike some meta data
echo "Adding meta data to post [$i]";
wp post meta update $post_id credit "Created automatically using wp-cli" --allow-root;
echo "----------------------------------"
echo "";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.