• Resolved fdiorazio

    (@fdiorazio)


    Been using wp-cli which I love. But i’m trying to create new posts w/ a clickable image, but 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 w/ 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?

    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.

    • This topic was modified 7 years, 2 months ago by fdiorazio. Reason: fixed code quote
    • This topic was modified 7 years, 2 months ago by fdiorazio. Reason: added double backticks to escape backticks
    • This topic was modified 7 years, 2 months ago by fdiorazio. Reason: 5 backticks for 1 backtick
    • This topic was modified 7 years, 2 months ago by fdiorazio. Reason: using ` as backtick
Viewing 4 replies - 1 through 4 (of 4 total)
  • Something like this should do it all at once:
    POST_ID=$(wp post create --post_title='TITLE' --post_status=publish --porcelain); ATTACHMENT_ID=$(wp media import ./FILENAME --post_id=$POST_ID --porcelain); wp post update $POST_ID --post_content='[gallery ids="$ATTACHMENT_ID"]'

    I think I can make it a bit shorter too. I’ll work on it for a bit and post back when I find something.

    UPDATE: Use this instead. I had the quotes messed up, so the bash variable wasn’t being substituted.
    POST_ID=$(wp post create --post_title='TITLE' --post_status=publish --porcelain); ATTACHMENT_ID=$(wp media import ./FILENAME --post_id=$POST_ID --porcelain); wp post update $POST_ID --post_content="[gallery ids='${ATTACHMENT_ID}']"; echo $POST_ID && echo $ATTACHMENT_ID

    Alright, here’s a much shorter (and better) solution:

    wp post create --post_title='TITLE' --post_content="[gallery ids='$(wp media import ./FILENAME --porcelain)']" --post_status=publish

    Thread Starter fdiorazio

    (@fdiorazio)

    Thanks so much. Although if I don’t use media import on a post ID, then the image ends up at SITEURL/FILENAME which then ends up as a 404, the thumbnail shows, but clicking on it is 404.

    However this is good enough because now I know to use “gallery ids” as post_content and won’t have to fiddle around with dates or paths.

    I played around with “gallery option” and removed the link with “gallery link=”none”” and changed the size, but size wouldn’t change (even with “full”) until I added a column of 1, yet the size was still limited to the page width of the theme, fine for now.

    [gallery ids="298" gallery size="full" gallery link="nonolumns="1"]columns="1"]

    Thread Starter fdiorazio

    (@fdiorazio)

    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:

    post-create.sh PATH_TO_IMAGE.FILETYPE “TITLE”

    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>"

    Thanks for your help, I think I’ll use [gallery options] in some other form in the future.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp-cli post create & media import issues’ is closed to new replies.