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 badges1 Answer
Reset to default 1When 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:
- 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.
- On the
init
hook, read the list of keys withget_option()
, and register a taxonomy for each one withregister_taxonomy()
. - 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. - For the values, add them as terms to the taxonomy using
wp_insert_term()
, but only after checking that value doesn't already exist withget_term_by()
. - 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.