admin管理员组

文章数量:1292285

Sometimes I have duplicate IDs between Post Types and Taxonomies, I know this happens because they are stored in different tables but I wish it was possible to have a unique ID for each thing in WP, is there a way to achieve this?

Sometimes I have duplicate IDs between Post Types and Taxonomies, I know this happens because they are stored in different tables but I wish it was possible to have a unique ID for each thing in WP, is there a way to achieve this?

Share Improve this question asked May 21, 2021 at 17:54 NicolaNicola 1278 bronze badges 4
  • 1 Which columns specifically are you referring to? I understand wp_posts.ID is one, but what is the other? Also, can you explain your use case here? I can't think of any reasonable means to do this, and I can't think of a reason to want to do this. – phatskat Commented May 21, 2021 at 18:07
  • @phatskat the tables I am referring to are wp_posts and wp_terms, the use case is for a translation meta-box that I show on both post and term screens. For each language the meta-box shows a select field where I can indicate which is the translation for that particular language. The select field is populated by terms on term admin screens and by posts on post admin screens. On the frontend I need to show a language switch, this would take the queried object id and show the link to the corresponding translated object. – Nicola Commented May 21, 2021 at 19:43
  • I think you should try restructuring your data. Maybe create a posts_translation table and a terms_translation table. The columns for each would be id | type | language | translation. Then when you make a translation, add a row for the given object ID and type (e.g. ID = 20, type = post or ID = 1001, type = term), translation, and language. When the language changes, use that reference table to pull the translated values. There are also plugins for this kind of thing. Beyond that, you should show what you've tried and where you're stuck if you want to get any useful answers. – phatskat Commented May 21, 2021 at 19:58
  • @phatskat that's an interesting approach, thanks. I just realized I can distinguish the query type by using get_queried_object() and get_class(), this would return either WP_Term or WP_Post, not unique IDs but at least I can tell each other apart... – Nicola Commented May 21, 2021 at 20:58
Add a comment  | 

1 Answer 1

Reset to default 0

The only way I could think to achieve this would be to query the DB and build a list of ids, then check to make sure none of the ids match, but that might be a very taxing query. You would also need to change the way the id's for each table would be updated which could get a bit messy, but I believe it's possible it would just take quite a bit of effort.

You might be able to use something like: https://developer.wordpress/reference/functions/wp_unique_id/ to increment your unique ids, but this will probably take a lot of effort.

本文标签: taxonomyUnique IDs for Post Types and Taxonomies