Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 885
Ray Walz Avatar asked Feb 17 '15 01:02

Ray Walz


1 Answers

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.

like image 108
William Turrell Avatar answered Sep 23 '22 01:09

William Turrell