Skip Navigation

[Resolved] Toolset view filters- disable radio buttons where the count is less than 1

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

Problem:
The customer asked how to disable the radio options in the views search form which can't produce any results.

Also wanted to keep the count of results next to those options unchanged.

Solution:
Guided about the options to disable or hide the options which can't produce results in the view's custom search settings.

Also explained that with this setting the count of the results next to those options would also update, each time search input changes, so shared some custom script alternative.

Relevant Documentation:
n/a

This support ticket is created 2 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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 6 replies, has 3 voices.

Last updated by Paul Marconi 2 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2292109
Capture.PNG

Hi there,

I have created a CPT called 'Team' and added few posts. I also used Toolset 'Custom Fields' to create a <select> dropdown list with the alphabet letters (a-o, for now) as the options. This 'Letter' field is a required field, so each Team post need to be assigned with a letter.
I have created a toolset view that uses the 'Letter' custom field as the filter (radio buttons). In the 'Search and Pagination' section, I have these codes:
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group">
<label for="wpv-wpcf-letter-select">[wpml-string context="wpv-views"]Letter Select[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-letter-select" type="radios" format="%%NAME%% (%%COUNT%%)" url_param="wpv-wpcf-letter-select"]
</div>
[/wpv-filter-controls]
[wpv-filter-end]

I have added '%%COUNT%%' in the format parameter in order to see if the count is greater or equal to 1 ( >= 1).
This is a testing page: hidden link

Since I am using the 'radio buttons' field type for the filter, I want to disable or add the text 'disable' to the <option> tags when the count is less than 0. I want to disable (not hide) the letters that has count 0, and only have the ones clickable when the count is greater or equal to 1.

Is there a way to do that using php or in the functions.php file?
Not sure how to use %%COUNT%% in php templates.

Thank you!

#2292523

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2022-02-15 at 07.35.32.png

Hi there

You can disable a filter input if it wouldn't produce any results, which I think is what you are aiming for here.

You are using the legacy Views editor, yes?

See the screenshot for the settings you need...

#2292581

Waqar
Supporter

Languages: English (English )

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

{ticket status updated}

#2293003

Thank you!
It did disable the inputs when it wouldn't produce any results.
Is there a way to include a default value to this radio button field?
I want to add an 'ALL' option as the default, so it will display all the posts/team members.

#2293025
search-after-position-is-selected.PNG
search-default.PNG

Hi @nigel,

I actually can add a default value 'ALL' to the radio button field by using this parameter 'default_label'. However, I am running into another issue.
If you go to this test page (hidden link), you can see there is a total of 3 Team Members.
There are 1 in 'B', and 2 in 'N' (screenshot attached).
Now if I select 'CEO' from the Position dropdown list, the count number changes.
The count for 'B' is now 0 and the input is disabled, and for 'N' it's now 1 (screenshot attached)

Is there a way to fix this?

Thanks!

#2294089

Waqar
Supporter

Languages: English (English )

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

always-show-all.png

Hi,

Thanks for writing back.

That is the expected behavior, when the option 'Show only available options for each input', is selected in the view's search settings.

With each search, the form is updated along with the count of options, to reflect how many options are there which can actually produce some results.

The only way to always show the total count of options would be to select the option 'Always show all values for inputs' (example screenshot attached). But, with this change, you'll also lose the ability to disable or hide the options with '0' results.

One workaround can be to include some custom JS script that disables the ones with the '0' count.

Here are the steps:

1. First, you'll need to include the count of the option in the format attribute for the 'wpv-control-postmeta' shortcode, in a way that it is added as a class name for a wrapping span tag ( e.g. format="%%NAME%% (%%COUNT%%)" would become format="<span class='%%COUNT%%'>%%NAME%% (%%COUNT%%)</span>")

Example:


[wpv-control-postmeta field="wpcf-letter-select" placeholder="All" url_param="wpv-wpcf-letter-select" format="<span class='%%COUNT%%'>%%NAME%% (%%COUNT%%)</span>"]

2. Next, in the view's JS editor, you can include this custom script, that disables all the options for the radio field 'wpv-wpcf-letter-select', with the count '0':


jQuery( document ).on( 'js_event_wpv_pagination_completed js_event_wpv_parametric_search_form_updated ready', function( event, data ) {
jQuery(".form-group span.0").parent("label").addClass('0');
jQuery(".form-group label.0 input[name='wpv-wpcf-letter-select']").attr("disabled",true);   
});

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2295869

Thank you Waqar! The code works!
My issue is resolved now.

Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.