Been using wp-cli which I love. But i'm trying to create new posts with a clickable image and the best wp-cli does with 1 command is a featured image which is not clickable (plus featured images are above my post's title & content).

This command is supposed to create a post with image, but instead it's an empty post (in the backend the image is "uploaded" to the post, but the image is not displayed):

wp media import ./FILENAME --post_id=$(wp post create --post_title='TITLE' --post_status=publish --porcelain)

Is there a fix or a missing option? Again I don't want to use the featured image option since it puts the image above my title/content.

So now I've moved on to 2 commands: Creating the post & then updating the posts' content field/value.

wp post update <ID> --post_content="<a href=""URL-TO-IMAGE""><img class=""size-full"" src=""URL-TO-IMAGE"" /></a>"

The problem with this is that I have to rearrange a bunch of values, so after I run this:

wp media import FILENAME

I'll need to do this:

echo `wp option get siteurl`/wp-content/uploads/`date +%Y`/`date +%m`/FILENAME

and so on

I'm running latest wp-cli & wordpress with the default plugins, so maybe I'm missing a plugin? Thanks.


So I just ended up doing a query on the database for the link of the image:

wp db query "SELECT guid FROM wp_posts WHERE ID='ID_FROM_MEDIA_IMPORT'" --silent --skip-column-names

I wrote this bash script for the whole process:


imageID=$(wp media import $1 --porcelain)
imageLINK=$(wp db query "SELECT guid FROM wp_posts WHERE ID='$imageID'" --silent --skip-column-names)
wp post create --post_title="$2" --post_status=publish --post_content="<a href=""$imageLINK""><img class=""size-full"" src=""$imageLINK"" /></a>"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.