Skip Navigation

[Resolved] Conditional to test for taxonomy results?

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Shane 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2355385

I'm trying to list associated taxonomy terms under posts in a view using the following code, and NOT display the word "Tags:" if there are none. The code that I'm trying to use (below) is not working; it will always come back as TRUE even if nothing is assigned to a post in those taxonomies. Is there another way I should be checking?

[wpv-conditional if="('[wpv-post-taxonomy type='school_subject,identity_markers,issue_areas' format='text']' ne '' )"]
<p>Tags: [wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"]</p>
[/wpv-conditional]

#2355435

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Thank you for getting in touch.

To check if a post has a specific term assigned then you will need to make use of the has_term() function.

Your conditional should look like this.

[wpv-conditional if="('has_term('school_subject,identity_markers,issue_areas','taxonomy')' ne '1' )"]
<p>Tags: [wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"]</p>
[/wpv-conditional]

Replace "taxonomy" with the slug of your custom taxonomy.

Thanks,
Shane

#2355613

This doesn't work, but perhaps I just wasn't clear in my question. has_term seems to check for specific taxonomy terms. I need to see if any terms were applied at all (from the multiple taxonomies - school_subject,identity_markers,issue_areas.

So displaying them with : [wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"]

works fine, but I want to somehow test to see if that would yield an empty result, and if so not display the "Tags:" text.

I tried testing for ([wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"] ne '' ) but cound't get that to work. Perhaps my formatting was off?

Thanks,
Bob

#2355653

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Can you enable the debug on the conditional shortcode by adding debug="true" ?

Also you can combine the conditional shortcode to check across multiple taxonomies.



[wpv-conditional if="('has_term('','school_subject')' ne '1' ) OR ('has_term('','identity_markers')' ne '1') OR ('has_term('','issue_areas')' ne '1')   "]
<p>Tags: [wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"]</p>
[/wpv-conditional]

Please let me know if this helps.
Thanks,
Shane

#2355687

That doesn't work. But again, isn't that the incorrect use of has_term, as I am checking for anything with a custom taxonomy, not a specific term?

I went back to this:
[wpv-conditional debug="true" if="('[wpv-post-taxonomy type='school_subject,identity_markers,issue_areas' format='text']' ne '' )"]
<p>Tags: [wpv-post-taxonomy type="school_subject,identity_markers,issue_areas" format="text"]</p>
[/wpv-conditional]

And get the below in the debug for an entry with no terms set in and of those custom taxonomies:

####################
wpv-conditional attributes
####################
Array
(
[debug] => true
[if] => ('' ne '' )
)

####################
Debug information
####################
--------------------
Original expression: ('' ne '' )
--------------------
After replacing 1 general variables and comparing strings: ('' ne '' )
Comparing to

And then there are some set:

####################
wpv-conditional attributes
####################
Array
(
[debug] => true
[if] => ('Woman' ne '' )
)

####################
Debug information
####################
--------------------
Original expression: ('Woman' ne '' )
--------------------
After replacing 1 general variables and comparing strings: ('Woman' ne '' )
Comparing Woman to

So I want to be able to display the "Terms:" text only when terms are set.

#2355705

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

It is being used correctly. The has_term function can check for specific taxonomy or if there is a specific term you want to check for.
See the documentation below.
https://developer.wordpress.org/reference/functions/has_term/

So the use context is appropriate for what you require.

You will need to use the debug attribute to see what exactly is happening in the conditional.

Thanks,
Shane