Skip Navigation

[Closed] What method is best to create the hierarchy? Taxonomy or relationships?

This support ticket is created 2 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 10 replies, has 3 voices.

Last updated by Waqar 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2279705

I am creating a website with a hierarchical structure that has objects related to the different levels of the structure.
The structure consists of organizations with departments up to ten levels deep.
One object is cars, another is employees. There are other objects as well.
What method is best to create the hierarchy? Taxonomy or relationships?

Truls

#2279759

Nigel
Supporter

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

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

Hi Truls

Naturally, the answer is "it depends".

You need to consider how you set up the hierarchical data, how you connect it to posts (which will be affected by whether you are doing so on the front end or back end), and what your requirements are when it comes to displaying the data.

Taxonomies have an advantage inasmuch as they are a core WP feature and well supported by 3rd party applications. So if you have a lot of data and need to import it, most import plugins would support taxonomies out of the box. The steps with importing data as relationships is more complex, and may involve paying for a premium plugin.

But a deep hierarchy of taxonomy terms may be difficult to manage. (I assume you are talking about a single taxonomy, where—to use a common analogy—the top level would be country, the second level would be state, the third level would be city etc., rather than using separate taxonomies for country, state, and city, because there is nothing to tie a city to a particular state or a state to a particular country.)

Having set up such a hierarchical taxonomy, when you came back to it in 12 months, could you remember what the 5th level in th hierarchy represented? How would you output all of the terms in the 5th level of the hierarchy if you needed to? (Lots of nested term queries.)

With relationships the hierarchy would be clear, because each level in the hierarchy would be a named post type, and if you wanted to output all of the objects of the 5th level of the hierarchy you would just be querying that post type (which you can do simply with a single View).

I suspect relationships may make more sense, but I think you need to plan how and where you'll need to input and display data to come to a clear-cut decision.

#2279865

Hi Nigel.
I have already tried out both taxonomy and relationships for the organization structure. Both works, but relationships seem more sleek and versatile.

My issue now is registering objects like cars and copy machines on the different departments in the organization. The postid for the organization is one master id, and the userid for the logged-on user is the other master id in my solution.

The user can only see the objects registered in their organization and only create new objects belonging to their organization.
My client wants to sort objects by type and by department.

Do I need to create a relationship between objects and every department level? There are 10 department levels in an organization.

Truls

#2280395

Hi,

Thanks for writing back.

Ideally, the object items ( like cars, copy machines, etc ), should be connected to only the individual department level that they belong to. But, I'm not exactly clear on how are you managing the department levels or hierarchy.

Are you using hierarchal department posts in a single custom post type "Departments" or are you planning to divide those levels into further custom post types?

If you could share some example data for these department levels, I'll be in a better position to guide you with the next steps, accordingly.

regards,
Waqar

#2280451

Hi Waqar
Is it possible to continue this conversation in private mode?
It is because of the nature of my contract with the client.

Truls

#2281627

Sure, I've set your next reply as private.

If you'd like to remove any particular information from the previous replies, let me know about that too.

#2283591

Hi
Do you need any more information from me?

Truls

#2283647

Thank you for sharing these details and I apologize for the delay in getting back on this.

Your understanding is correct and Toolset Forms don't have a built-in feature that can be used to populate related options on the fly, based on the user's field selection. Deploying something like this is possible, but, will require a fair amount of customization.

Here is an example of the process of adding a new object to a department:

1. User will see a form to enter a new object post. Through this form, you can collect basic information about the object like title, type, etc. At this point, you'll not collect information about its level or department.

2. After creating the object post, the user will be redirected to the created object post's single page, where there will be an edit form to edit that same created post.

In this form, you'll include a generic select type field to select the "Level". It will have the levels posts, related to the current user's organization. You can use a custom shortcode to generate these options, programmatically. For this, you can either use a view's output in PHP code or simply a "get_posts" query:
https://developer.wordpress.org/reference/functions/get_posts/

Here is an example support forum reply, where a similar setup was used to populate generic field options, programmatically:
https://toolset.com/forums/topic/split-publishing-related-many-to-many-posts/#post-1215323

3. Next, you'll need another generic select type field to select the "Department". It will have the department posts, related to the level selected by the user in the first field and its options can also be populated programmatically, like the first field.

4. The challenge would be that the "Department" field's options need to be dependent on the selection made in the "Levels" field. When the page will load, you'll know where to get the "Levels" from, but not the "Departments", because that selection is yet to be made.

To overcome this, you have these two options:

a). AJAX:
In the AJAX approach, you'll write custom JS and PHP functions to detect changes in the "Levels" field and populate the options of the "Departments" field every time the level selection is changed.

Here are some guides on using AJAX in WordPress:
hidden link
hidden link

b). Non-AJAX:
In this approach, instead of using a single edit post form for selecting the levels and departments, you'll use two separate edit forms, one for each.

The first form will include the generic field "Levels" and the second one will include the generic field "Departments". On the form submission of each of these forms, you'll make sure that the page refreshes to stay on the same single object post page. This way, you'll be able to show the correct related "Departments" options based on the selected "Level".

5. Whether you adopt the AJAX or the non-AJAX approach, the last step would be to process the "department" selected in the generic field and connect it with the current object post, when the form is submitted, using the "cred_save_data" hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

You'll also find an example function for this in the forum reply, I mentioned earlier.

I hope this makes sense and please let me know if any step or point is not clear.

#2287821

Hi Waqar
I almost got it right. The only thing left is the Ajax (I would like to use Ajax) approach to populate the selects. The first select was easy. The organization (top-level parent) is set in a “cred_save_data” call on the “New Object” form.
On the “Edit Object” form after the redirect, the second level select shows the correct values. I have a third-level selection where I have to use Ajax. I have looked through several forums and support discussions, but the solution eludes me. Can you point me to a support thread or discussion that can help me?
Truls

#2288559

Thanks for the update and glad that you've been making progress.

Although 1-1 customization assistance of this scale is beyond the scope of support that we can provide over the forum, we do our best to share some pointers, wherever possible.

Can you please share temporary admin login details, along with the link to the page with this form?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2289645

Thank you for sharing the access details, but, I'm seeing the critical error message on that link.

I'll assume that you're working on the code and making changes. If you're having difficulty in making AJAX call work in WordPress, it would be more efficient to post your challenges at dedicated WordPress community forums:
https://wordpress.org/support/forums/
https://wordpress.stackexchange.com/

If you need assistance around using any of the Toolset functions within those AJAX calls, please let me know about the exact section of code to review and I'll see if I can suggest some improvements.

The topic ‘[Closed] What method is best to create the hierarchy? Taxonomy or relationships?’ is closed to new replies.