Skip Navigation

[Closed] generate a taxonomy from a custom post type

This support ticket is created 7 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 7 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#414348

I have run into the same problem multiple times while working with Toolset. I think there needs to be a way to generate a taxonomy from a custom post type and have that taxonomy updated as changes occur to the corresponding CPT.

Let's call this "Dynamic Taxonomies" (killer name, right?!)

As an example, let's say I have a website selling cars and car related stuff. First, I need a CPT for cars to store the make, year, color, mileage, etc. about all of the cars that I have.

My site also sells car stereos. These stereos come in different sizes and only fit certain cars. I can create a taxonomy for cars and then edit each radio, checking all of the car taxonomy entries in which that radio will fit. The problem is that I need to manually maintain that taxonomy as the post type cars changes.

Conversely, I can create a radios taxonomy and edit each car, choosing the radios with which it is compatible. But I still need to maintain the radio taxonomy manually, as radios come and go.

What Toolset really needs is this: A way to make a dynamic taxonomy from a CPT. So, if I generate a dynamic taxonomy from cars, the list of taxonomy elements matches the list of cars, including any parent-child nesting.

If I remove a post from cars, the corresponding taxonomy element is also removed, along with all of its associated (now orphaned) meta fields are also removed. Most importantly, when I edit a radio post, a taxonomy appears, showing all of the available cars, so I can pick the cars it fits from the current list of available cars.

Maybe I'm just not up to speed with WordPress, but it sure seems like this could be useful.

#414495

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Patrick

Thanks for your post. While I could follow your use case example readily enough, when I discussed it with our developers they were unreceptive, I'm afraid. They said the duplication of the CPT as a taxonomy was indicative of a problem with the data structure.

I've been trying to think of how you might implement something similar using Toolset, although I can't see how you would without relying on some custom code to supplement it.

My first thought was to use post relationships—that your car stereo posts could be children of your car posts, so that you assign a car as a parent of the stereo. The problem there is that, although a post can have multiple parents of differing post types it can only have one parent of a particular post type, meaning your car stereos could only ever be said to work with one specific kind of car.

I think the best solution, then, is to describe the connection between your cars and radios using custom fields. You could add a repeatable field to your radio posts that stores the cars with which that radio will work (and you can add as many as are relevant). You would probably want to store them as the post ids of the cars.

Automatically populating the custom field select box with your list of car posts is what you want, but where you start having to rely on a custom solution.

You would need some custom PHP to query your car posts and generate an array of your car data. You then need to make this available to some custom JavaScript on the front-end that uses it to populate the select box accordingly (whether you are operating in the WP admin pages or on a front-end CRED form).

How do you get the array from your server-side PHP to your client-side JavaScript?

Do you know about wp_localize_script?

Here's the official documentation (https://codex.wordpress.org/Function_Reference/wp_localize_script) and a tutorial (hidden link).

I don't know what your coding abilities are but if it is something that occurs often then it may be worth persevering to get it working. Alternatively, we maintain a list of certified Toolset partners for custom work: https://toolset.com/consultant/.

#414582

Yes - I have created linking custom post types before. That's a post type that's a child of 2 other post types. In the case of my cars and radios problem, a third post type that's a child of both card and radios allows for many-to-many relationships and does create the type of linking necessary.

The problem with this is complexity. It would take either enough custom code to remove orphaned child posts, or, as probably happens most often, just letting those orphans build up in the database.

There has to be a simpler, more straight forward way of relating posts of one type to posts of another type, and it sure seems like taxonomy is a convenient means of doing that. The only thing currently missing is an automated way to syncing the elements of a taxonomy with posts of a specific type.

I would imaging an additional pane on the page where a CPT is defined, where the user could choose whether an automatic taxonomy for that post type should also be created and maintained.

Two other issues would be key to this: First, the pane where taxonomy is selected for a post type would then need to prevent users from adding new taxonomy terms, since this would now be controlled by adding new posts to the type controlling the taxonomy. The second issue is that each time a post with an automatic taxonomy is deleted, the meta records for associating any posts to that posts automatic taxonomy term would also be automatically removed.

In practice it would work quite well. If I remove a radio post, the term disappears, along with all of the links from car posts to that term, so it would simply not appear as an option on any car post.

What I'm trying to achieve is a simple mechanism for relating posts to other posts where those relationships are automatically removed when a related post is removed. I'm thinking that a taxonomy makes sense because the nature of this function needs to be many to many, or at least one to many when used while editing a post.

Let's say I have a list of products as one post type. Then, lets imagine I have a list of projects in which one or more of those products are used. The only way I can link those products to the projects in which they're used is to either create a third post type to link between the two post types, or to create a taxonomy matching the elements of one post type that is used to relate records of the other post type.

The fundamental problem with both of those methods is that they both require manual maintenance - unless sufficient additional code is written to make it automated. And the problem with that additional code is that the entire approach is already too complicated for what's needed. What's needed is a simple, built-in mechanism to relate posts to other posts which automatically removes those relationship records when their corresponding posts are removed.

I think this is sitting right in front of us, but we've all been too close to our own code to see it!

#414586

Check this out: hidden link

#414596

There should be a choice for a custom post field that is checkboxes, radio buttons, or a select, where the options automatically match the posts of whatever type is selected.

I see the questions about how to populate a dropdown with the posts of a CPT in this list all the time. The solutions are hard to build and hard to maintain. But the think they are all trying to accomplish is really simple. They just want to be able to link the post they're editing to one or more posts of another type.

This is a fundamental necessity when working with any type of relational data and having to manually create and maintain this function is a missed opportunity for Toolset. Please ask your developers to reconsider!

#414606

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Patrick

I have answered tickets about automatically populating dropdowns with lists of posts or users before and I understand and am sympathetic to your reasoning.

I will put it to our developers again and let you know when I hear back from them.

The topic ‘[Closed] generate a taxonomy from a custom post type’ is closed to new replies.