Add tags/categories to a WordPress post from WP-CLI

I have a bash script that posts to WordPress. It does this fine, but I want to have it add tags to the posts. It uses WP-CLI and there doesn’t seem to be a way to add tags to a post from it.

To clarify, I’m talking about post tags, not HTML tags.
Also, this bash script is being run on the same server that is hosting the WordPress site.

Read More

I also found blogpost.py, however it doesn’t seem to have a tag feature either.

From what I’ve read, I think I would be able to accomplish what I want using XML-RPC in some way, but I know almost nothing about it.

Here’s the WP-CLI command I’ve been using. Which works, but doesn’t add tags.

wp post create --post_title="This is the title" --post_content="This is the post" --post_status=future --porcelain

In desperation, I tried adding --post_tags="these are tags", but it didn’t work. I’ve been searching for the answer for days now.

I just want to be able to make a WordPress post, with tags and a category, from a cronjob. I’m familiar with bash, so that would be best, but if you know of any solutions using other languages, that’s welcome also.

Thanks.

Related posts

Leave a Reply

2 comments

  1. As you may know, post tags are a WordPress taxonomy (like categories) called post_tag.

    You need a set of subcommands of wp post, specifically wp post term, to edit them.

    You also need WP-CLI v0.18.0 or above – older versions don’t have it.

    In all these examples, 1 indicates the post ID.

    Add one or more tags to any existing tags

    Separate multiple tags with spaces (not commas.)

    wp post term add 1 post_tag foo
    Success: Added term.
    

    “Set” one or more tags

    This will replace (overwrite) any existing tags for the post.
    You’d use this instead of remove followed by add (one fewer command necessary.)

    Separate multiple tags with spaces.

    wp post term set 1 post_tag bar baz
    Success: Set terms.
    

    List current tags

    wp post term list 1 post_tag
    
    +---------+------+------+----------+
    | term_id | name | slug | taxonomy |
    +---------+------+------+----------+
    | 7       | bar  | bar  | post_tag |
    | 5       | baz  | baz  | post_tag |
    +---------+------+------+----------+
    

    Remove tag(s)

    wp post term remove 1316 post_tag baz
    

    You can also use this for categories or any other taxonomy.

  2. This can now be done more simply. In today’s latest version of wp-cli (2.4), tags_input is an option of wp post create and update. You can therefore use

    wp post create file --tags_input="Tag1,Tag2,Tag3.."
    

    or

    wp post update (postID) --tags_input="Tag1,Tag2,Tag3.."