Skip Navigation

[Resolved] Problem with a checkbox filter in a view

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

Last updated by katinaA 1 year, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2483245

We are trying to add a checkbox filter to a view. We already have 3 of these filters set up based on Taxonomies and those are working fine. We are now trying to set it up based on a custom field and that one is not working.

You can see the page where this is set up here:

hidden link

Filter by Region, Filter by Guided Activities Offered and Filter by Facility Amenities are working. Filter by Partner Awards is the one that is not working. If you check one of the boxes for 2021 or 2022 and apply that filter, all partners still display as results. The filter isn't applying.

You helped us get a conditional output display working on this Awards field on these pages:

hidden link
hidden link

The Partners present on these pages are what should show up on the Find A Partner page if you only apply either the 2021 or 2022 Award filter. Instead, all partners continue to display. Any assistance you can provide will be appreciated. I'm sure it's probably just a setting change that I'm missing.

#2483667

Waqar
Supporter

Languages: English (English )

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

Hi,

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

To troubleshoot this, I'll need to see how this view and its search filters are set up in the admin area.

Can you please share temporary admin login details, in reply to this message?

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

regards,
Waqar

#2485345

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing the access details and I apologize for the delay in getting back to this.

I've performed some tests with checkbox type field search on your website, as well as on mine and the results are consistent and as expected.

1. If I select the '2021 Award Winner' option, all the posts which have this checkbox checked for the awards field are shown.
2. If I select the '2022 Award Winner' option, all the posts which have this checkbox checked for the awards field are shown.
3. If I select both options, all posts which have any one of those options checked, are shown.

This means that for case 3, which is multiple selected options, by default, the "ANY" operator is used and not the "AND".

To change that operator to "AND", you'll need some custom code attached to the "wpv_filter_query":
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )


add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
	// process if specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 11371) ) {
		if( !empty($query_args['meta_query']) ) {
			foreach( $query_args['meta_query'] as $key => $value){
				if($key != 'relation') {
					foreach ($value as $subkey => $subvalue) {
						if(is_array($subvalue)) {
							if($subkey != 'relation') {
								if ($subvalue['key'] == 'wpcf-partner-awards') {
									$query_args['meta_query'][$key]['relation'] = 'AND';
								}
							}
						}
					}
				}
			}
		}
	}
	return $query_args;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2485861

Note that in your list above, #1, selecting 2021 is not resulting in the partners with this option checked being shown. Many more partners are being shown as 2021 winners than should be shown.

Something more is not right with everything with this awards field still, but now I'm not sure where the problem lies.

Take a look at this Partner:

hidden link

It is showing on the page as being a 2021 and 2022 Award Winner. But if you go to the Edit Page for this Partner, you'll see that neither option is selected in the award field. So when a search is done using the Award checkbox filter and selecting 2021 or 2022 on the Find A Partner page here:

hidden link

This 10 Can partner is showing up as an award winner when they shouldn't be.

That is why I thought the new filter for the View wasn't working correctly. But I'm now thinking the problem might lie elsewhere. I have to figure out why the field is showing that 10 Can is an award winner for both years on the Partner page when neither option is selected. This is not good because this functionality was working fine before. Something that was done to get the filtering working has broken this in some way. This makes the problem much more complicated than I thought it was.

On our Partner Template page, we have the following code to display the Award Winner graphics conditionally on if the Partner has either of the options for that field selected:

[wpv-conditional if="('[types field='partner-awards' separator=', '][/types]' ne '')"]<div class="tb-field" data-toolset-blocks-field="1" data-last-update="1.4">Partner Awards:<br>[types field='partner-awards' state="checked" option="0"]<img src="hidden link">[/types]
[types field='partner-awards' state="checked" option="1"]<img src="hidden link">[/types]</div>[/wpv-conditional]

We changed the values on the Awards field options to get query filters working on the Views in a previous ticket. That ticket is here:

https://toolset.com/forums/topic/continuation-of-closed-ticket-setting-up-a-new-view/

So changing the values of the field options has broken the code above. I'm wondering if that might have something to do with why more Partners are showing than should be showing with this checkbox filter.

Another problem I'm now realizing and seeing can be seen here:

hidden link

My client asked me to set up a display of the number of results from each View on this page. I did so and thought I had it working properly, but now I'm realizing that the count number is not accurate. I used [wpv-found-count] for this. I know you want us to report different problems in different threads, but I thought this might have something to do with what is wrong here. I'll start a new ticket for this issue. I'm just wondering if this count being off has something to do with why the results on this checkbox filter are showing too many.

It seems like everything with this Awards field is still causing problems. This is getting very frustrating because now it seems like things we did to get the query filter working in the previous ticket lined above have now broken other functionality that was working fine before. I'm not faulting you in ANY way on this. I realize this is complicated and things done for one fix can have unintended consequences. I just didn't expect this to be as difficult as it is proving to be. I think where we changed the values of the field options to get the query filter working broke the code pasted above from the page template. So now more is needed to solve everything.

I did add the custom code you provided in the code snippets section and called it awards-checkbox-filter-custom-code. But now I'm thinking we need to figure out the page template problem first to make sure the field is working as it should at that level before proceeding to figuring out why the checkbox filter is showing too many results. Understood if you have to split this ticket. I hope this doesn't get to be too much to solve. I thought setting this up would be easy but now my head is spinning trying to keep it all straight. Just to help you with it all, we have the following displays related to this field:

1) On the Partner template, we display whether a Partner is an award winner or not.

2) On the Awards pages, we display all Partners that are award winners using Query filters along with wanting to have count numbers for each one.

3) On the Find A Partner page, we want to have a checkbox filter to only show award winning partners among the results.

Sorry this is getting this complicated.

#2486231

Would it be easier if I change this field to a Taxonomy? We have all filters working for Taxonomies. So I thought maybe that would make this easier. Something about trying to do this on a custom select field with checkbox options has proven extremely complicated.

It would be A LOT of work changing this field to a Taxonomy because I'll have to re-select the options on hundreds of Partner posts. But I'm starting to resign myself to that being the best approach here. At the time I defined the field, I didn't know the client was going to want it as another filter option on the Find A Partner page. That was something that arose later. Had I known it was going to be another field added to the checkbox filter options on that page, I'd have set it up as a Taxonomy from the start.

If I do this, I'm going to have to rebuild every View utilizing this field as well. But I'm starting to think that might be easier than getting this approach to work.

#2486241

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing this update.

It is strange that the custom code from my last message worked on my test website, but not on yours.

Even before reading your most recent message, I was thinking along the same lines, that it would be more efficient in every way, to switch the checkboxes type custom field for awards, with a custom taxonomy.

I can understand that it would mean redoing a number of elements. But considering all the recent challenges that we've discussed in your recent tickets around this particular field, this switch seems like the most logical thing to do and should be totally worth it.

#2486245

I'm sure the discrepancy between the sites is something I've done wrong, but I don't know what that might be.

OK, I'll go ahead and change it to a Taxonomy, but I will need help with getting the Partner template working again to display the award images. That's this code currently using the field:

[wpv-conditional if="('[types field='partner-awards' separator=', '][/types]' ne '')"]<div class="tb-field" data-toolset-blocks-field="1" data-last-update="1.4">Partner Awards:<br>[types field='partner-awards' state="checked" option="0"]<img src="hidden link">[/types]
[types field='partner-awards' state="checked" option="1"]<img src="hidden link">[/types]</div>[/wpv-conditional]

This functionality does the following:

1) If a Partner has won no awards, then nothing displays, including no display of the Partner Awards:<br> header.
2) If a Partner has won either a 2021 or a 2022 award, then the single image for that award displays.
3) If a Partner has won both 2021 and 2022 awards, then both images for both awards display.

I'll go ahead and set up the new Taxonomy and I'll use a few Partners initially for testing. I'm going to have to rebuild all Views previously using this field as well as changing this template code. But we'll take it one step at a time. The first thing will be getting the template displaying as it should. Understood if you have to wait until I get the Taxonomy built before you can provide replacement code for the template code to accomplish the same thing using the Taxonomy instead.

#2486309

I've built the new Taxonomy. I'm trying to create the new block in the Partner template now. I've gotten as far as setting up the conditional as follows:

[wpv-conditional if=" NOT ( empty( '[wpv-post-taxonomy type="award" format="slug" ]') ) " ][/wpv-conditional]

I think I did that correctly to where it won't display anything if no options are selected with this taxonomy.

But now I'm stuck and unsure how to get it to display the desired images instead of the Term Name. Here's what I tried:

<div class="tb-field" data-toolset-blocks-field="1" data-last-update="1.4">[tb-dynamic provider="__current_post" post="current" source="post-taxonomies" field="award" separator=", " ]</div>

That just gets it to display the Term Names that are selected in the Taxonomy. How do I get it to display the images replacing the Term Names?

#2486329

I've now confirmed that all filtering, both the query filters on the region views and the checkbox filter on the Find A Partner page are now working properly after converting the field to a taxonomy and redoing all the views.

And it took a lot less time than I feared it would because I found a way to bulk edit the partners.

So now all I need for everything to be done is the replacement code mentioned above for getting the taxonomy term name to be replaced by a defined image.

#2486983

Waqar
Supporter

Languages: English (English )

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

Glad that the restructuring went well.

For showing a different image for each term of the "Awards" taxonomy, a neater approach would be:

1. You can add a new taxonomy term "Award Image" from WP Admin -> Toolset -> Custom Fields -> Term Fields. This will allow you to attach an image with each of the terms in this taxonomy.

2. Next, using a taxonomy view, you can show any information like the "Award Image", from only the attached terms attached to the current post, as explained in this forum reply:
https://toolset.com/forums/topic/split-showing-different-images-for-different-taxonomy-terms/#post-1965179

I hope this helps and please let me know if you need any further assistance with this.

#2487207

OK, I believe I have set up the images for each Awards taxonomy term correctly. That's very cool that you can do that, so thanks for showing me that. I may have other applications for this functionality now that I know how to do it.

But now I'm trying to add this to the page template and am not seeing how to get the image to display in place of the term name.

I'm using the Single Field element, and choosing Taxonomy as the field type. But I'm not seeing any way to display this new Award Image field that is attached to the taxonomy terms.

Rereading the thread that you linked in #2 above, I'm seeing that I need to create a view and then I can add that view in the page template. Let me try that.

OK, I set up a new view called Award Taxonomy Image Display. I think I did this correctly. Then I went back into my Partners page template and tried to add a new View block. But when I tried to select this new view, I didn't see it as a selectable option. I see Post Views as a header with all my previous views showing in there. Then I see Taxonomy Views below that but there is no option for Award Taxonomy Image Display. I only see a blue rectangle under Taxonomy Views where I expected to see Award Taxonomy Image Display as a selectable option. When I click on the blue rectangle, it then shows this in the template:

Error while retrieving the View preview. The selected View (ID: undefined) was not found.

Is there something I need to do to make this Taxonomy View accessible to the template editor?

#2487703

Waqar
Supporter

Languages: English (English )

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

The taxonomy and the user views are not supported by the "Views" block in the Gutenberg editor.

You can instead use a "Fields and Text" block to call this taxonomy view, using the "wpv-view" shortcode, like this:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-view )


[wpv-view name="Award Taxonomy Image Display"]

Notes:

1. I've included it in the "Template for Partners" and it seems to be working correctly.

2. The taxonomy view's output was showing the social sharing icons because a content template is being used to show it in the view's loop. I've added the suppress_filters="true" attribute to its shortcode in the view so that the extra content filters are not applied to it:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-body )


[wpv-post-body view_template="loop-item-in-award-taxonomy-image-display" suppress_filters="true"]

#2487833

It's working perfectly! I had to reverse the sort order and place the view shortcode within the conditional as we wanted it, but it's showing the correct results for all combinations.

That social media bit attaches itself to the bottom of every view result and I've been killing it with display: none; CSS. Good to know suppress_filters="true" will do the trick. I've now also found a couple settings to change within the AddToAny settings that will hopefully stop them from appearing in that way. I want those to appear once at the bottom of every page, but I think I had the settings incorrect and they were displaying at the bottom of every view result as well.

The only remaining problem with this is I can't figure out how you got it to display within the block editor. Did you just know the code needed from experience? When I click to add a View block, this Award Taxonomy Image Display is not among the options and I still see only that blue rectangle under the Taxonomy View header. How do I get this view to show as usable in there? If I need to use this view elsewhere, I know I can just copy and paste your code, but I'd like to get the block editor to allow me to use it in case the same thing happens with any subsequent views I set up. Understood if this needs to be split to a different ticket.

#2488733

Waqar
Supporter

Languages: English (English )

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

Let me explain this with a bit of a background.

1. The views can be categorized into 3 types, in terms of the list that they show:
a). Posts views
b). Taxonomy views
c). User views.

2. And there are 2 types of views, in terms of which editor is used for creating them:
a). Classic/legacy editor
b). Blocks editor (in the Gutenberg editor)

All 3 types of views can be created using the classic/legacy editor, however, only the post views can be created and displayed using the "Views" block in the Gutenberg editor.

To show the other two types of views (i.e. taxonomy and user views), you'll have to use the shortcode "wpv-view" that I shared in my last message.
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-view )

As explained in the documentation link above, this shortcode's format is very simple and you just have to provide the target view's name or slug in the 'name' attribute.

But, if you're using a "Fields and Text" block, you can also use its "Add" button, to insert the same shortcode through the selection wizard.
Screenshot 1: hidden link
Screenshot 2: hidden link

I hope this makes it more clear.

#2489609

Yeah, fully understood now, but there's no way any of us Toolset users would know this instinctively. I have noticed that there are times when I prefer using the classic/legacy editor and times when I prefer the blocks editor as far as ease of getting things to work goes. So this will be another one of those occasions. I'd definitely suggest trying to get it to where all options are usable in both if you can.

No problem doing it the shortcode way now that I know that is necessary. I was thinking something was wrong with my site because you do have the Taxonomy View header in there above the blue rectangle I'm seeing. I thought that meant I should be seeing the Taxonomy View I was trying to use and something was wrong preventing it from being in the selectable options. That's very confusing. I'd say that you should hide that Taxonomy View header from displaying to avoid that specific confusion. It will still be confusing why Taxonomy Views are not present in there, but it will be less expected for them to be there with that header being gone.

Thanks again for all the help. We're good to go now. The last thing left is my current ticket for migrating the site. We're ready to get this site finally launched now.

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