Skip Navigation

[Resolved] Count existing terms, and use the result in HTML conditions

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

Problem:
I want to count all terms in a taxonomy and depending on the result display things using HTML conditions.

Solution:
The only solution is either using what is provided here https://toolset.com/forums/topic/conditional-on-there-being-more-than-one-item-in-a-taxonomy-assigned-to-a-post/#post-1321835 or WordPress's native wp_count_terms() function (https://codex.wordpress.org/Function_Reference/wp_count_terms), but latter does not allow to filter terms by post.
It counts the actual number of terms in the entire taxonomy, disregarding the post.

The solution here instead allows to count terms per post:
https://toolset.com/forums/topic/conditional-on-there-being-more-than-one-item-in-a-taxonomy-assigned-to-a-post/#post-1321835

This support ticket is created 5 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by johnB-43 5 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1321069

I am trying to: Use a conditional to display a text if a post has more than one item from a taxonomy assigned.

For example, this item hidden link has two languages assigned to it, and I would like a note to display saying 'you need to check if specific subjects are taught in the right language'

But this item hidden link only has one language assigned, so this note is unnecessary.

The display options for adding a field about this taxonomy only allow me to select the number of items in each category, not the number of categories assigned to the current item.

How could I do this? Thanks!

#1321081

You should be able to create this logic by checking if the post has at least one term assigned (for the cases it has one term or more):
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/#one

Now your case requires also a check if it has two terms (but not more?) assigned.

In any case you'll hence need to count the terms assigned. Toolset could count the terms in a Taxonomy view, that returns the terms assigned to the post where the view is displayed.
Using a Raw Loop Output, this value returned (the amount of terms) could be used in a HTML conditional.

Or, you can create a Custom ShortCode or function that uses some code like this example:
https://wordpress.stackexchange.com/questions/326030/how-to-check-if-a-post-has-at-least-2-terms-from-a-custom-taxonomy-attached

First, you'd get_the_terms() https://developer.wordpress.org/reference/functions/get_the_terms/ to get all terms assigned to the post.
You need to pass it the Post ID, and the string of the taxonomy of which you want the terms of.
That will give you an array, now you can count() that array (this is a native PHP function hidden link)
This will return the amount (numeric) of terms assigned to the post.

You can use this either in a Custom ShortCode, or Custom Function, and return the result from count().
For ShortCodes, you can refer to these examples: https://codex.wordpress.org/Shortcode_API

Now, wether you use a ShortCode or function that returns your count() result, you would have to register this in Toolset > Settings > Front End Content > ShortCodes or Functions of third parties

Then, you can use your Custom ShortCode or function in a HTML condition, for example, checking if the count() value is greater than one, or two, as shown here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

Please let me know if this helps.

#1321383

Thank you! That is a lot of options! 😀

I think the simplest would be the first. But I would need to look into more than one term assigned. Basically, I need a logic of:

[wpv-conditional if="('[ NUMBER OF TERMS IN A TAXONOMY ]' > 1 )"] DISPLAY THIS TEXT [/wpv-conditional]

This is easier if you want it to be > 0, as then you can use the ne '' . But how do I make it > 1 ? Or in other words, how do I get Views to print NUMBER OF TERMS IN A TAXONOMY ?

#1321835

As I explained above, Toolset could count the terms in a Taxonomy view, that returns the terms assigned to the post where the view is displayed.
Using a Raw Loop Output, this value returned (the number of terms) could be used in an HTML conditional.

Let me make an example:

1. Create a View, and choose to query Taxonomy (your taxonomy)
2. In the query filter section select "Taxonomy Term" and "Set by the current post". Save it and proceed to the Loop editor
3. In the Loop editor (just below it) find the "Disable the wrapping DIV around the View" checkbox and check it
==> This is what will make our View return a "raw" loop output
4. In the loop insert nothing (leave it empty), but just before (or after) the <wpv-loop> tag, insert the Count ShortCode of Toolset:
[wpv-found-count]
(https://toolset.com/documentation/user-guides/views-shortcodes/#vf-155378)
==> This will show the total of terms found for the post where the view is inserted.

Now create a small shortcode to ensure the data your view produces, is really just a clean number.
You can add this to the functions.php of your Theme or Toolset Settings Custom Code:


function ensure_numeric_taxonomy_count_of_posts() {
	$args = array(
          'title' => 'the title of the view'
	);
	$value = render_view( $args );
	return intval($value);
}
add_shortcode('taxonomy-count','ensure_numeric_taxonomy_count_of_posts');

Edit the "the title of the view" adequately to match your view.

Then Register the new shortcode "taxonomy-count" in Toolset > Settings > Front End Content > 3rd Party ShortCodes

This now allows you to insert a HTML Conditional like this on your single posts, or the Content Template with which you display those:

[wpv-conditional if='( "[taxonomy-count]" eq "2")']Notice for 2 Tags on post[/wpv-conditional] 

Of course, you can check for greater than (gt) or lower than (lt) and other operators, see https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Note, the above solution includes a custom code because it's a particular situation, where you use a View just to count amounts of items and even if the View is able to return the loop clean, the amount of items is wrapped in some space because outside of the loop (and raw loop applies only to the loop itself)
I missed that in my first evaluation.

Given we use Custom Code (but a small one) above, the same shortcode could also just use get_the_terms() as I explained before, count() them and return that value.
It would have the same exact effect.

Using a view of course you have many more options fo queries and other kinds of manipulation of the result 🙂

Hope that helps!

#1324555

Thanks! Before I give this a go, a quick question (since this seems pretty complicated for something that doesn't seem like it should be so hard): could I cheat by focusing on something else? For example, if an entry has two or more items from a taxonomy assigned, those items display comma-separated.

Could I write a conditional that says: if a string containing all the taxonomy items for the displayed post contains at least one comma (and thus at least two terms, then display 'text'? Would that work? Some syntax of the shape:

[wpv-conditional if="('[ .div_containing_the_taxonomy ]' includes ',' )"] DISPLAY THIS TEXT [/wpv-conditional]

#1325013

Toolset HTML conditional doesn't support includes operators.
But you can set up HTML conditionals to check whether the post includes the term or not:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#comparison-operator > "If you are using taxonomy as a field you can use include the term and don’t include the term operators."

But you do not want to know if a specific term is assigned. You want to know if the number of terms in taxonomy is greater than one, display something.

The only solution is either using what I provided here https://toolset.com/forums/topic/conditional-on-there-being-more-than-one-item-in-a-taxonomy-assigned-to-a-post/#post-1321835 or WordPress's native wp_count_terms() (https://codex.wordpress.org/Function_Reference/wp_count_terms), but that does not allow to filter by post.
They count the actual terms in the entire taxonomy, disregarding the post.

#1327019

That is fair enough! Thank you for the detailed explanation and the step-by-step instructions, this really helped. I followed your suggestions and it works a dream now 🙂