Skip Navigation

[Resolved] How to make view (without sub-view) for complex taxonomy terms retrieving?

This support ticket is created 3 years, 2 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 5 replies, has 3 voices.

Last updated by BV 3 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2188779

BV

In several places I use the system of 2 levels of views for location terms retrieving.

For example, to get all countries, which are children of several continents, I use 2 views:
- parent view - retrieve all continents' taxonomy terms (terms with no parent)
- sub view - retrieve all children of term passed from parent view

Or another example: to retrieve all big cities in the USA (big cities are usually children of states, when small cities - are under big cities), I use 2 views:
- parent view - retrieve all states taxonomy terms (terms with parent "USA")
- sub view - retrieve all children of term passed from parent view

In some cases I need to display the tree structure in template, so that 2 levels of loops is ok.

But in some cases I need to display just the list of terms, which I receive from sub view. Like list of all countries - not separated by continent. Or List of all big cities of US - not separated by state. It's possible to do in 1 SQL request, just by transferring the parent id. But via Toolset Views GUI it's obviously impossible.

Is it possible to pass the list of parent terms to the sub view and get all groups of children in one SQL request?

#2188881

Nigel
Supporter

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

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

I can't quite tell from your description exactly what the set up of your taxonomies are, but if you have a taxonomy with just a 2-level hierarchy ( e.g. continents < countries) and you want a View to simply return all countries, you could set a query filter where filter you need is that the term is childless. (Continents have child terms, countries do not.)

You can't do that directly in the GUI (it only allows you to set a condition such as the parent is none), but—rather than looking at directly constructing SQL queries—you can use the API filters to modify the taxonomy query generated by the View.

See https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

And see the WordPress documentation for the arguments you can use with get_terms: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

It looks to me like you need to set childless to true in the query args.

#2188885

BV

Nigel, you got my request correctly, except of I have multileveled taxonomy - Continents->Countries->States->Big Cities->Villages.

And for one case I need to get all Countries but with 1 resulting request. For another case - Big Cities. I can't defer them by present/absent of parent.

But the continents and states - is the permanent list, so I'd want to make a request of all terms, whose parents are in list of IDs (list of all continents, for example, or list of all states).

#2188967

Nigel
Supporter

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

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

I suggest you look through the available arguments in https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

To my eyes, it looks like the only relevant arguments you can set are child_of, parent, or childless.

You can use parent = 0 to retrieve the top level terms.

You can use childless = true to retrieve the bottom level terms.

For everything in between it seems clear that you must use more than a single query, likely starting at the top and iterating down with a new query for each level in the hierarchy.

The way WordPress stores the data I can't see an alternative.

#2189479

{ticket status updated}

#2189711

BV

My issue is resolved now. Thank you!