WP-CLI

Editing multiple posts at once with WP-CLI

What if "wp post update" accepted more than one post ID? It does!

I already wrote about WP-CLI here early, but I’d like to share about the post update command today.

For years, I’ve been using wp post update to update a unique post per time by running wp post update followed by the post ID and whatever I want to edit.

$ wp post update 123 --post_author=456

The command above will update the post ID 123 to apply the author 456 to it.

Although that command is really efficient, it’s kinda boring to run multiple command instances to apply the same information (post author, for instance) to multiple posts.

Currently, these are the command examples in the WP-CLI documentation:

$ wp post update 123 --post_name=something --post_status=draft
Success: Updated post 123.

# Update a post with multiple meta values.
$ wp post update 123 --meta_input='{"key1":"value1","key2":"value2"}'
Success: Updated post 123.

As I had focused on the examples, I never paid much attention to the command description::

wp post update
Updates one or more existing posts.

WP-CLI Documentation

Updates one or more existing posts.

Yup! The examples in the documentation don’t cover editing multiple posts at once (wp post delete does mention that), but the wp post update command can edit more than one post ID.

How cool is that?
Now it is no longer necessary to run the same command several times just changing the post ID.

Examples

To update more than one post with only one command, you just need to inform the posts ID like this:

$ wp post update 123 456 --post_author=789
Success: Updated post 123.
Success: Updated post 456.

That command will update the posts ID 123 and 456 to apply the author ID 789 to them.

What if I need to edit all posts in a given post type? You can achieve that by nesting two commands like this:

$ wp post update $(wp post list --post_type=page --format=ids) --post_author=123
Success: Updated post 123.
Success: Updated post 456.

In this command, all pages (--post_type=page) will be updated to receive the author ID 123.

Giving back to the community

As I know there are more people like me out there paying more attention to examples than to descriptions, I sent a Pull Request adding the examples to the WP-CLI documentation. The new examples will be published on the next WP-CLI release šŸ™‚

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: