最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Twin value tags as an esoteric taxonomy

programmeradmin1浏览0评论

I suspect the answer might be, get ready to write some SQL but I'm going to ask anyway.

I have a situation where I am pulling data from an outside source and storing it in a custom post type. For the main, this looks like it will work very well. However, there is a slight bump in the road.

The data comes with what are called stances. They exist as key-value pairs where key and value could be anything. Given that I store other data in the meta already, what I was hoping for was something like tags but in pairs. A taxonomy that works exactly like tags in most respects but has two values.

I toyed with converting the values into $tag = "{$key}|{$value}"; but I can see that getting messy and bloated really fast.

The current solution puts the entire array into a single meta. It works but it is not great for "select all posts of type X that agree foo is bar".

Which brings me to ask if a more complex two-factor taxonomy is even possible?

I suspect the answer might be, get ready to write some SQL but I'm going to ask anyway.

I have a situation where I am pulling data from an outside source and storing it in a custom post type. For the main, this looks like it will work very well. However, there is a slight bump in the road.

The data comes with what are called stances. They exist as key-value pairs where key and value could be anything. Given that I store other data in the meta already, what I was hoping for was something like tags but in pairs. A taxonomy that works exactly like tags in most respects but has two values.

I toyed with converting the values into $tag = "{$key}|{$value}"; but I can see that getting messy and bloated really fast.

The current solution puts the entire array into a single meta. It works but it is not great for "select all posts of type X that agree foo is bar".

Which brings me to ask if a more complex two-factor taxonomy is even possible?

Share Improve this question asked Jun 12, 2019 at 15:40 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

When importing you could create a taxonomy for each unique stance key, then add the value as a term to the taxonomy for that key, then assign that term to the post.

I'd suggest something like:

  1. Store the stance keys somewhere in the database (probably as options), making sure that you don't have duplicates. Depending on how many you have, this could probably just be a single value with a serialised array.
  2. On the init hook, read the list of keys with get_option(), and register a taxonomy for each one with register_taxonomy().
  3. When importing, save any new keys to the database, and immediately run register_taxonomy() for them so that the taxonomy is registered for the rest of the process.
  4. For the values, add them as terms to the taxonomy using wp_insert_term(), but only after checking that value doesn't already exist with get_term_by().
  5. Once the taxonomy has been registered, and the term for the value has been inserted or found, assign the term to the post using wp_set_post_terms().

If you do that then you can easily filter posts based on these values with a taxonomy query. This will be faster than attempting to filter using post meta.

发布评论

评论列表(0)

  1. 暂无评论