Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wp post term * to support Term IDs #3716

Closed
pkarjala opened this issue Jan 6, 2017 · 5 comments
Closed

Allow wp post term * to support Term IDs #3716

pkarjala opened this issue Jan 6, 2017 · 5 comments

Comments

@pkarjala
Copy link

pkarjala commented Jan 6, 2017

Currently if you want to set the Terms associated with a Post via wp-cli, you have to use the Slug of the Term. This is somewhat cumbersome if there are a large number of Terms to set, and the ID of said Term is already known, as the Slug must be programmatically looked up and then the ID translated to that Slug.

Instead, allow the option of passing an array or list of Term IDs to the wp post term * command list.

See https://codex.wordpress.org/Function_Reference/wp_set_post_terms for documentation on using IDs in wp_set_post_terms.

@danielbachhuber
Copy link
Member

Thanks for the suggestion, @pkarjala.

Instead, allow the option of passing an array or list of Term IDs to the wp post term * command list.

Historically, the challenge with supporting both slugs and ids is that a term can have a numeric slug. As such, if I have a term with id 3 and a term with slug 3 and run wp post term add post_tag 3, WP-CLI couldn't know which term you meant without additional information.

Here are some prior threads:

Thoughts?

@pkarjala
Copy link
Author

pkarjala commented Jan 8, 2017

One option, as previously suggested, may be to set a flag that indicates ONLY usage of Term IDs versus Slugs. In that instance, the user MUST provide only numeric Term IDs; otherwise it is presumed that they are providing a Slug (which may also be a numeric string value).

I'm not familiar enough with other flag settings in the wp-cli project to know what existing standards are, but here are some suggestions:

wp post term <post_id> --term_ids=<array or comma separated list of term ids>
wp post term <post_id> <2, 3, 4, 5, 6> --term_ids=true

@danielbachhuber
Copy link
Member

Do you think it's necessary to add this feature? How did you end up solving the problem in your case?

@pkarjala
Copy link
Author

pkarjala commented Jan 9, 2017

We used

$document_type = json_decode( shell_exec( "wp term list document_type --format=json --fields=term_id,name,slug" ) );

to pull a list of all Terms associated with a specific Taxonomy (document_type in this case), and their associated IDs and Slugs. We then process this in PHP to create a list of Slugs which we use for comparison against the existing list of Term IDs that we have from a spreadsheet. The Name is included to make sure we have the correct IDs and Slugs.

It would be more convenient and faster programmatically for us to be able to use the Term IDs directly instead of having to do a lookup comparison for each Term ID, but it is not strictly necessary.

@danielbachhuber
Copy link
Member

This makes sense. I'm amenable to adding a --by=<type> argument that accepts either 'slug' or 'id' and the default is 'slug'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants