Skip Navigation

[Résolu] How can I get ONLY the parent taxonomy terms for a VIEW output

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a nested View structure where the outer View shows posts and the inner View shows terms from a hierarchical taxonomy. The nested inner View should be filtered so that only the terms associated with each post in the outer View loop are displayed in the inner View. I would also like to hide any terms that have hierarchical parent terms.

Solution: Use two filters in the nested View. One Query Filter should be based on the term parent, where the term parent is None. This will show only top-level terms from the hierarchy. The other Query Filter should be based on the taxonomy term, and should be set by the current post. This will hide unrelated terms from each nested View.

This support ticket is created Il y a 3 années et 4 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : ,

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par PaulS4783 Il y a 3 années et 4 mois.

Assisté par: Christian Cox.

Auteur
Publications
#2132053

For example if I have a custom post type with the nested hierarchical custom taxonomy for "Countries"

parent - Asia
child - Japan
child - China
child - Thailand

If I just query [wpv-post-taxonomy type="category"] it will return ALL the categories e.g. Asia / China
Is there a way to limit the query to ONLY the top level (parent) taxonomy terms e.g. Asia

#2132915

Hi, the wpv-post-taxonomy shortcode unfortunately is not flexible enough to handle that kind of filtering. Instead, you should create a View of the Countries taxonomy using the legacy Views editor, and add a taxonomy term parent Query Filter. If you cannot see the Query Filter panel, scroll to the top right corner of the view editor screen and click the "Screen Options" tab. You can turn on the Query Filter panel here. Then add a Query Filter based on the term parent, where the term parent is None. The results of that View will be top-level terms like Asia, in your case, since they have no parent terms.

If you are not familiar with the legacy View editor, you can enable it in Toolset > Settings > General. Look for the Editing Experience setting, and choose the option that enables both the Block Editor and the legacy editing experience if you'd like to have the option of either editor. Once you've activated the legacy experience, reload the page and you'll find a new main menu item Toolset > Views. You'll create and edit standalone Views from that dashboard.

I can help you implement the legacy Views editor if you need asssitance. We don't have much documentation available for that experience anymore since the push to Blocks.

#2134245

Thanks. I am familiar with the Views Legacy editor. In fact I prefer to use that since I rarely use Gutenberg.

So I have a created a view [wpv-view name="home-page-post-teasers"] to output a list of the ten most recent posts of a custom post type.

The view contains the nested view [wpv-view name="parent-taxonomy-terms" cached="off"] which queries a custom taxonomy "regions" with the taxonomy parent filter:

Select taxonomy terms whose parent is None.

lien caché

The problem is that it outputs ALL the parent taxonomies rather than just the one which is relevant to the current loop item in "home-page-post-teasers".

Am I missing something?

#2134883
Screen Shot 2021-08-05 at 12.09.30 PM.png

The problem is that it outputs ALL the parent taxonomies rather than just the one which is relevant to the current loop item in "home-page-post-teasers".
If you also want to filter by the terms applied to the current post in the parent View, you must add another Query Filter, a taxonomy term filter. You will have one taxonomy parent filter (parent equals None) and one taxonomy term filter (set by the current post). I'm attaching a screenshot here to show this setup. The combination of both filters should produce the results you expect here.

#2135221

Perfect! You nailed it.
Thanks for the help.