Skip to content

Instantly share code, notes, and snippets.

@bikubi
Last active November 8, 2017 11:03
Show Gist options
  • Save bikubi/75c57f732105dd7c56ec160917055eb9 to your computer and use it in GitHub Desktop.
Save bikubi/75c57f732105dd7c56ec160917055eb9 to your computer and use it in GitHub Desktop.
Quickly post an one-image-only post via WP-CLI.
#!/bin/bash
# Quickly create/queue an one-image-only post, tumblr-style.
img="$1"
title="$2"
status="$3"
category="image" # change this!
author=1 # also this.
if [ -z "$img" ] || [ ! -f "$img" ] ; then
echo "USAGE: $(basename $0) IMAGE [TITLE [queue|draft|publish|...]]"
exit 1
fi
if [ -z "$title" ] ; then title="($category)" ; fi
if [ -z "$status" ] || [ "$status" == "queue" ] ; then
# check future posts; add one day
nextdate="$(date -d "$(wp post list --post_status="future" --category_name="$category" --fields=post_date --posts_per_page=1 --format=csv --porcelain | grep -o "[0-9: \-]\+") 1 days" '+%F %T')"
status="future"
else
nextdate="$(date '+%F %T')"
fi
tmpimg="/tmp/${category}_$(basename "$img")"
ln -sv "$img" "$tmpimg"
post=$(wp post create --post_title="$title" --porcelain --post_status="$status" --post_date="$nextdate" --post_author=$author)
if [ ! $post ] ; then
echo "could not create post, got: $post"
exit 2
else
echo "created post $post"
fi
# uncomment those options for newer versions of wp-cli
wp media import "$tmpimg" --featured_image --title="$title" --post_id="$post" # --preserve-filetime
wp post term set $post category "$category" # --by=slug
rm -v "$tmpimg"
# move image to a subdir
donedir="$(dirname "$img")/posted"
mkdir -v "$donedir"
mv -v "$img" "$donedir/"
mv -v "$img.out.pp3" "$donedir/" # RawTherapee sidecar file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment