Skip Navigation

[Resolved] In VIEWS is there a way to test whether any taxonomy terms exist?

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View of posts in which I have nested another View of taxonomy terms. I would like to filter the View of taxonomy terms so that only those terms applied to the current post in the loop are displayed. If no terms are found associated with the current post in the loop, that post should not be displayed in the results.

Solution: Use the legacy View editor to create and manage nested Views, since this feature is not supported in the Block editor. In the inner nested View, add a taxonomy term Query Filter that filters by the current post. In the Loop Editor, insert the wpv-taxonomy-title shortcode in the loop to display the term title, or use other shortcodes to display other information like the term slug, term archive link, or custom term fields. Include information about the parent post in the wpv-items-found section using shortcodes, but do not place parent post shortcodes inside the wpv-loop tags. Anything in the wpv-loop tags will be repeated for each matching term.

Relevant Documentation:
https://toolset.com/course-lesson/creating-a-view/#filter-the-list-of-posts
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created 3 years, 4 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
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)

This topic contains 4 replies, has 2 voices.

Last updated by PaulS4783 3 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#2132049

I am creating a custom view and would like to do something like this:

[wpv-conditional if="{any custom taxonomy tag terms exist}"]
<span class="taxonomy-link">Tickers: [wpv-post-taxonomy type="ticker"]</span>
[/wpv-conditional]

#2132913

Hello, the legacy View system offers the ability to create a View of taxonomy terms, though that feature does not exist in Views created in the Block editor. You could use a View of any taxonomy much like a conditional, to determine whether or not any terms exist in that taxonomy yet. Create a View and select the taxonomy you want to test in the Content Selection panel. In the Loop editor area, you can use the wpv-items-found section to display something specific if terms exist. If no terms exist, you can display something else or nothing at all. You should leave the wpv-loop section empty, otherwise, anything between the wpv-loop tags will be repeated once for each term in the results. Example loop editor contents:

[wpv-layout-start]
	[wpv-items-found]
        
        <span class="taxonomy-link">Tickers: [wpv-post-taxonomy type="ticker"]</span>
	
        <!-- wpv-loop-start -->
	<wpv-loop></wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

Then place this View in the location where you want to display the Tickers information.

#2134293

Sorry, I don't really follow what that code is doing.
I tried the code below, but similar to another ticket you are helping with, it returns ALL the taxonomy terms rather than just testing for "Ticker" taxonomy terms associated with each loop item in the parent view.

[wpv-layout-start]
	[wpv-items-found]
    <strong>Tickers:</strong>
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-ticker-terms"] 
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>Ticker: [wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]
#2134865
Screen Shot 2021-08-05 at 11.53.00 AM.png

Oh I see, sorry but your initial comment didn't mention a parent View so I didn't realize that was involved here. In this case, you should apply a taxonomy term Query Filter in the nested terms View. The taxonomy term filter should display terms filtered by the current post (screenshot attached). That will filter each iteration of the terms View so that only the terms associated with the current post in the outer loop are included in the results.

If you cannot see the Query Filter section in the View editor, scroll to the top right corner and click "Screen Options". You can enable the Query Filter panel there.

#2135225

Well done! Thank you for helping me find the solution.