Skip to content

Conversation

@caribe
Copy link

@caribe caribe commented Apr 11, 2025

Fixes this 10 year old issue wp-cli/wp-cli#1323 (closed, but not fixed).

wp post create parameter --tax_input is supposed to be an array, but no JSON parsing is done and therefore is unusable.

Fixes this 10 year old issue wp-cli/wp-cli#1323 (closed, but not fixed).

`wp post create` parameter `--tax_input` is supposed to be an array, but no JSON parsing is done and therefore is unusable.
@caribe caribe requested a review from a team as a code owner April 11, 2025 15:55
@codecov
Copy link

codecov bot commented Apr 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@swissspidy
Copy link
Member

Thanks for opening this PR.

Would you also be up for adding some tests to cover this change? See #133 for inspiration.

@swissspidy
Copy link
Member

swissspidy commented May 6, 2025

I just added a test now, but it fails because tax_input requires the current user (which there technically isn't one with WP-CLI) to have the necessary capabilities. So we need to figure that one out first...

Related: https://github.com/orgs/wp-cli/discussions/6052

And save STDOUT as {CAT_2}
And I run `wp term create post_tag "Term One" --porcelain`
And I run `wp term create post_tag "Term Two" --porcelain`
And I run `wp post create --post_title='Test Post' --post_content='Test post content' --tax_input='{"category":[{CAT_1},{CAT_2}],"post_tag":["term-one", "term-two"]}' --porcelain`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy, in this scenario, would passing --user=admin in this step be acceptable in your opinion?

Suggested change
And I run `wp post create --post_title='Test Post' --post_content='Test post content' --tax_input='{"category":[{CAT_1},{CAT_2}],"post_tag":["term-one", "term-two"]}' --porcelain`
And I run `wp post create --post_title='Test Post' --post_content='Test post content' --tax_input='{"category":[{CAT_1},{CAT_2}],"post_tag":["term-one", "term-two"]}' --user=admin --porcelain`

This user is the default administrator when core is installed during testing:
https://github.com/wp-cli/wp-cli-tests/blob/5b42c271c96efd1072cecaa8b2923db52318b056/src/Context/FeatureContext.php#L1392

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would fix the test but not the user experience in general.

We would need to document that tax_input only works in combination with --user, which is odd.

Another option could be to temporarily filter user_has_cap for this particular code path so that it would always work. But not sure if there's precedence in WP-CLI.

Perhaps @schlessera has a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can find some context in the discussion here: #207

This is a bug in the capabilities configuration of WordPress Core, and it basically means that tax_input (which is supposed to be generic) will ONLY work for custom taxonomies, but not for the built-in ones. So if you switch from category to a custom taxonomy, it should work without administrator access.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I had thought about adding a custom work-around in WP-CLI, but also wanted to have this flagged as a bug in WP Core. Not sure what the latest status is for Core.
But I'm happy to consider a workaround. At the time, I thought that tax_input should defer to post_tag or post_catgory for those, but I can't remember how involved that would have been.

@github-actions github-actions bot added bug command:post-create Related to 'post create' command labels Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:post-create Related to 'post create' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants