This command will generate posts and create a field with key "bar" and value "foo"

wp post generate --format=ids --count=10 | xargs -d ' ' -I % wp post meta add % foo bar

This does not seem to work:

wp post generate --format=ids --count=10 | xargs -d ' ' -I % wp post meta add % foo bar ; wp post meta add % key_2 value_2

How could create 2 meta fields and values after the wp post generate command?

up vote 1 down vote accepted

You just need to change the syntax of xargs to run multiple commands using the same placeholder:

wp post generate --format=ids --count=10 | sed -e "s/ /\n/g" | xargs -n1 -I % sh -c 'echo "Adding fields for %"; wp post meta add % foo bar; wp post meta add % key_2 value_2';

Update:

The output of wp post generate is post ids with spaces for the delimiter. I'm sure there is a better way for xargs to process spaces but I'm using sed to replace the spaces with newlines.

Reference:

  • I get: Error: Too many positional arguments: 2467 2468 2469 2470 2471 2472 2473 foo bar Error: Too many positional arguments: 2467 2468 2469 2470 2471 2472 2473 key_2 value_2 – BenB Jul 14 at 21:35
  • @BenB - Updated the code above to mutate the data in the pipe before it hits xargs. – jgraup Jul 14 at 22:30
  • Works like a charm! – BenB Jul 15 at 17:14

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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