Skip Navigation

[Resuelto] Only showing selected taxonomy terms in a Views Query Filter

This support ticket is created hace 1 año, 7 meses. 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 – 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 respuestas, has 2 mensajes.

Last updated by Simon hace 1 año, 7 meses.

Assigned support staff: Waqar.

Autor
Mensajes
#2083499

Tell us what you are trying to do?
- We have a Views query on this page: hidden link
- It has a keyword search and a taxonomy filter
- The nature of the data we have imported into the website means that we have a lot of unnecessary taxonomy terms in the taxonomy 'Media'. If you look in the dropdown, we only want the 'plain English' terms e.g. Craft, and not the terms that are codes
- For practical reasons, we can't manually delete the codes we don't want (they will come back in a future import) or stop them from being imported
- We have added a checkbox field to the taxonomy which we have ticked (set to 1) ONLY for the terms we want to display

QUESTION: Is it possible to filter the Views query dropdown so it only shows the terms where the custom field is set to 1?

If that's not possible, is there any other workaround you can suggest?

Thank you

#2084227

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

I'm afraid, there is no direct method available for this filtering, so it will require some workaround.

1. First, you'll create a taxonomy view that will list all these "Medium" taxonomy terms that you're using for filtering.

2. In the "Loop Editor" section of this view, you'll build the output in such a way that:

a). A conditional wrap exists that shows only those terms, where the custom term field value is 1.
( for this example, I'm using a field with slug "allowed", but you'll need to replace it with the one used on your website )

b). For the terms which are allowed to show through the conditional checks, you'll show their term slugs within a custom CSS block, so that in the main view's category filter drop-down options with those specific term slug as values, are visible.

Example of the complete content in the "Loop Editor" section:


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
	[wpv-conditional if="( '[types termmeta='allowed' output='raw'][/types]' eq 1  )"]
	<style> .wpv-filter-form select[name='wpv-medium'] option[value='[wpv-taxonomy-slug]'] { display: block; } </style>
	[/wpv-conditional]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	[/wpv-no-items-found]
[wpv-layout-end]

3. To ensure minimal output from this new taxonomy view, please also check the "Disable the wrapping DIV around the View" option, just above the "Output Editor" section.

4. Now, getting back to the edit screen of the original search view, you'll include some custom CSS code in the CSS editor, that will hide all the options except for the first default option from the category select drop-down filter:


.wpv-filter-form select[name='wpv-medium'] option {
display: none;
}
  
.wpv-filter-form select[name='wpv-medium'] option[value='0'] {
display: block;
}

5. The last step would be to include the newly created taxonomy view's shortcode at the start of the "Output Editor" section of the original search view:


[wpv-view name="view-to-show-list-of-terms"]

Note: Please replace "view-to-show-list-of-terms" with your actual taxonomy view's name/slug.

The end result will be that the custom CSS code from the main search view will first hide all the term options from the category select drop-down filter. But then the custom CSS code from the taxonomy view will show options for all those terms where the allowed field is checked (i.e. has value 1).

I hope this helps and please let me know if any point or step is not clear.

regards,
Waqar

#2087991

My issue is resolved now. Thank you!

#2092373

For anyone else trying this solution:
- It works well on desktop but it should be borne in mind that on mobile browsers on iOS (Safari and Chrome) the browser takes over rendering of select dropdowns, so the CSS is ignored and all the categories we wanted to hide were displayed
- As a result, we ended up doing an additional hack where we hid the filter entirely on iOS and showed a backup View that just listed all the categories as links to their category archives