0

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?

1 Answer 1

1

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:

3
  • 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, 2018 at 21:35
  • @BenB - Updated the code above to mutate the data in the pipe before it hits xargs.
    – jgraup
    Jul 14, 2018 at 22:30
  • Works like a charm!
    – BenB
    Jul 15, 2018 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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