Skip Navigation

[Resolved] How to hide Taxonomy that has no related post

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 – 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 4 replies, has 2 voices.

Last updated by Waqar 8 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2688362

I have 1 view showing a list of item Taxonomy items, and another view that shows list of posts that relate to the taxonomy items. I can't figure out how to hide taxonomy items that don't have related posts.

Specifically, I've created a custom post called Researches, and I've created two taxonomies for Researches; Grant Categories and Year Taxonomy

In one page, I want to show all the Researches that is of Grant Category: "Cluster Research Grant", grouped by Year Taxonomy: with output that is supposed to look like this:

2020
Adoption of Digital Technologies
Financing Schemes
2019
[some research title]

Problem now is that, the output also shows 2023, 2022, 2021 which does not have any related Research post associated which that taxonomy value. I cannot remove the 2023- 2021 years because I have Researches posts that have those years taxonomy but are Grant Category: "Small Research Grant"

I've reached the above stage by following this: https://toolset.com/forums/topic/how-can-i-group-views-by-custom-field/

This is the page I'm building now: hidden link

#2688367
screenshot-tax-view-options.png

Hi,

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

The taxonomy view's 'Query Options' has an option 'Don't show empty terms'.
( example screenshot attached )

You can enable that to exclude the terms with no posts attached to them.

regards,
Waqar

#2688375
showing years with no items.JPG
dont show empty items.JPG

Thanks for the reply. I've tried the suggestion but it doesn't work.

#2688395

Thank you for sharing this update.

Can you please share temporary admin login details so that I can see how this view is set up in the admin area?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2688557

Thank you for sharing the access details.

The option 'Don't show empty terms' isn't enough in this case, because the nested post view ( 'Cluster Grant Researches by Year' ) is filtering by the 'Years Taxonomy' and the 'Grant Categories'.

To make this work, I added the following changes to the view 'Years group for Cluster Grant Researches':

1. I added a div container around each result of the taxonomy view so that the custom code can easily target each one:
( screenshot: hidden link )


....

<div class='year-tax-container year-tax-container-[wpv-taxonomy-slug]'>
          <h4>[wpv-taxonomy-slug]</h4>
          [wpv-view name="cluster-grant-researches-by-year" year-tax="[wpv-taxonomy-slug]"]
</div>

.....

2. In the same view's 'JS editor', I included the following custom script:
( screenshot: hidden link )


jQuery(document).ready(function( $ ) {
  $('.year-tax-container').each(function()
  {
    if($('a', this).length == 0) {
        $(this).remove();
    }
  });
});

This code cycles through each container for the taxonomy results and removes the ones that don't have any link tag in them.

#2688561

Wow... that works exactly as I wanted. Thank you very much for your help. I won't be able to come up with that solution myself.