Skip Navigation

[Resolved] Filtering Taxonomy View Output based on Number of Posts of a Given CPT

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.

Our next available supporter will start replying to tickets in about 0.64 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 8 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2686558

I have a View of a taxonomy that is shared between two different post types, however the focus of the View is on one of the custom post types called "Custom Products". I'd like to filter out terms that do not have Custom Product posts tied to them, however since the "Do not show empty terms" setting accounts for all posts associated with the term, regardless of post types, this presents an issue with the output of my View when one post type has posts associated with a term but the other one does not.

The reason I have this taxonomy shared between two post types is to reduce redundancy and confusion and because there will be shared data between them.

This is the page which displays this View. As you can see, terms that do not have Custom Products posts tied to them still appear in my View output: hidden link

I'd like to avoid having to split the taxonomy up into two different taxonomies if I can absolutely help it, so if there is any way to accomplish this task within the scope of Toolset support, I would be greatly appreciative. Thanks in advance!

#2686620

Hi,

Thank you for contacting us and I'd be happy to assist.

I've done some research and couldn't find any built-in feature in Toolset or WordPress in general, that returns the count of posts attached to a term, based on a specific post type.

To achieve something like this as is, a custom function that uses the 'get_posts' query or a post view that uses a post type and a taxonomy filter will be needed to get the desired count for each term. But, I'll not recommend a solution like this, since it will increase the number of queries and will result in the performance impact.

If two post types are set to share a taxonomy, then I'll assume that they are expected to share posts across the terms too, if not now, at least in the future. In this case, the displaying of those terms with a '0' count shouldn't be a concern.

But, if you don't see these terms to share posts across these post types ever, then it means that it is more logical and intuitive to not attach a taxonomy with a post type that is not expected to have posts in that taxonomy's term.

I hope this makes sense and let me know if you need further assistance around this.

regards,
Waqar

#2686717

Minesh actually assisted me with using a View's output in a conditional argument here: https://toolset.com/forums/topic/effectively-utilizing-conditional-logic-with-multiple-arguments/

Using this new shortcode function, I've established a conditional that displays the loop output if the Custom Product Count View does not equal 0.

[wpv-conditional if="( '[render-view name='custom-product-count-conditional-arg']' ne '0' )"] [...] [/wpv-conditional]

This works sufficiently to remove a loop item from the output if the term in question does not have any related posts associated with it (see here: hidden link), with the caveat that the [wpv-found-count] shortcode is not accurate to the number of visible terms, but I think I'm willing to work with that, given that the majority of these terms will likely need to be visible at all times anyway.

Hopefully this method won't impact performance in any way, though.

#2686790

Thank you for sharing this update.

Yes, this is the solution that I had in mind too and this will consume the number of views queries equal to the taxonomy terms.
( this means that the more terms, the more the views queries will be to find and compare the count )

You can continue using it and unless you have hundreds of taxonomy terms to check for, the performance impact should be ignorable.

#2686856

We shouldn't have too many terms to deal with in this application, so it sounds like we'll be fine. Thank you for your help.