[Resolved] how to check whether a taxonomy term has published posts?

This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Related documentation: Adding Custom Taxonomies to WordPress and Creating Archives

This topic contains 9 replies, has 2 voices, and was last updated by  williamm-5 2 months, 1 week ago. The support staff assigned to this topic is Luo Yang.

Viewing 10 posts - 1 through 10 (of 10 total)
Author
Posts
#557985

Tell us what you are trying to do?

I wan to conditional display taxonomy terms based on whether it has published posts. The result should be display in the view.

Is there any documentation that you are following?

by view

Is there a similar example that we can see?

http://www.wpbeginner.com/wp-tutorials/how-to-display-a-category-only-if-it-has-posts-in-wordpress/

What is the link to your site?

no

#557989

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

Supporter Timezone: Asia/Hong_Kong (GMT+08:00)

Dear Williamm,

There isn't such a feature within Types plugin.

But if you have a copy of Views plugin
https://wp-types.com/home/views-create-elegant-displays-for-your-content/
you can use shortcode [wpv-taxonomy-post-count] to get the the number of posts for the current taxonomy:
https://wp-types.com/documentation/user-guides/views-shortcodes/#wpv-taxonomy-post-count

Then you can use it insider the [wpv-conditional] shortcode to display what you want depends on the result of [wpv-taxonomy-post-count], see our document:
https://wp-types.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

If you want to display terms which are assigned to posts, you can create a taxonomy view, with option:
Don't show empty terms
see screenshot empty-terms.JPG

You can also built the query with PHP codes manually, see wordpress document:
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
parameter:
'hide_empty'
(bool|int) Whether to hide terms not assigned to any posts. Accepts 1|true or 0|false. Default 1|true

#557995

Dear Luo,

Thanks very much for your quick response.

Still need you help, and I don't know how to use toolset to conditional display as you told as your mention below:

<If you want to display terms which are assigned to posts, you can create a taxonomy view, with option:
Don't show empty terms
see screenshot empty-terms.JPG>

following attached are my setting and it shows nothing.

1. result1.jpg is showing the result which is "display post view.jpg" sort by field child post count.
2. result2 .jpg is showing the result which is "display post view.jpg" sort by child post id.

actually, this is no post under "医药品" term.

waiting for your instruction. Thanks.

#558003

Luo, sorry

you have already answer my question perfect. Thanks.

Actually, my question is not been asking in the right way.

There is a child post link to parent post which has taxonomy.

I want to know how to check whether a taxonomy term has published child posts?

thanks and sorry for misleading.

#558019

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

Supporter Timezone: Asia/Hong_Kong (GMT+08:00)

In your screenshot:
https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2017/08/557995-display_taxonomy_view.jpg?x71388
The taxonomy view, you have already enabled the option "Don't show empty terms", so it should not output those term which do not have any posts assigned,

The problem you mentioned above is abnormal, we need to check if there is any compatibility problem in your website, I suggest you try this:
1) Make sure you are using the latest version of Types + Views plugin
2) deactivate other plugins and switch to wordpress default theme, and test again
3) If the problem still exists, please provide downloadable URL for the database dump file of your website in below "private detail box", also point out the problem page URL and view URL, where and how I can see the problem, I need to duplicate same problem and debug it in my localhost, thanks

#558033

Dear Luo,

This is no problem with toolset types plugin.

I try to use following code to check the view return result. But it is not work.

[wpv-conditional if="( '[wpv-view name="sidebar-product-market-detail"]' ne 'No items found' )"]
<h3>[wpv-taxonomy-title]([wpv-taxonomy-post-count])</h3>
<div>[wpv-view name="sidebar-product-market-detail"]</div>
[/wpv-conditional]

how could i get the return result from the view [wpv-view name="sidebar-product-market-detail"] in the view design panel.

Thanks.

#558050

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

Supporter Timezone: Asia/Hong_Kong (GMT+08:00)

Views will output results in a HTML div tag, if there is any result, it will output something like this:

<div class="xxx">
No items found
</div>

If ther
So the shortcode [wpv-conditional] you mentioned above won't be able to work.

As I mentioned above:
you have already enabled the option "Don't show empty terms", so it should not output those term which do not have any posts assigned, but it does not work in your website, it is abnormal, we need to check if there is any compatibility problem in your website, I suggest you try this:
1) Make sure you are using the latest version of Types + Views plugin
2) deactivate other plugins and switch to wordpress default theme, and test again
3) If the problem still exists, please provide downloadable URL for the database dump file of your website in below "private detail box", also point out the problem page URL and view URL, where and how I can see the problem, I need to duplicate same problem and debug it in my localhost, thanks

#558051

Thanks Luo,

As you said: Views will output results in a HTML div tag.

Can we just let Views output raw.

For example:
just output "No items found"

rather than

<div class="xxx">
No items found
</div>

thanks in advance!

#558060

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

Supporter Timezone: Asia/Hong_Kong (GMT+08:00)

No there isn't such a built-in feature within Views plugin, Views is not a data provider, it is not recommended to use view's shortcode insider the [wpv-conditional].
I suggest you move the taxonomy shortcode and post count shortcode insider the view "sidebar-product-market-detail", section "Loop Output Editor", wrap them into shortcode [wpv-items-found] ... [/wpv-items-found], see our document:
https://wp-types.com/documentation/user-guides/views-shortcodes/#wpv-items-found
wpv-items-found
Meta HTML wrapped in [wpv-items-found][/wpv-items-found] will be output if there are any posts | taxonomies | users returned by the View.

#558067

Luo, thanks very much ! I will reopen a thread for my issues. Thanks.

Viewing 10 posts - 1 through 10 (of 10 total)

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.