Skip Navigation

[Résolu] Assigning a value to a View filter using a checkbox with less-than comparison

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created Il y a 6 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 13 réponses, has 2 voix.

Last updated by Joff Il y a 6 années et 10 mois.

Assisted by: Nigel.

Auteur
Publications
#522792

I am trying to create a search filter on a view as a checkbox input type, using a "less-than" comparison function (<).

I want to use the checkbox to filter out results on a "distance" field, to show posts with a distance of less than 30, but I don't understand where I enter the value to compare against.

#522911

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Hi Joff

Checkbox filters are not envisaged to be used in such a way, they are intended to be used with Types checkbox fields (where the value is set on the field itself), not with numeric fields.

You could add the search filter as a textbox (where your users enter a "Maximum distance" value), or you could add a select dropdown where you have multiple values they could choose from that you enter manually when inserting the field (see screenshot where I am adding a max employees filter to a company search).

If you really wanted to use a checkbox, your only alternative is to use the Views API (specifically wpv_filter_query) to modify the query parameters for a search where you added a checkbox filter with the equals comparison and then used the API filter to change the query as required.

See https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#523078

Hi Nigel,

Ok, that makes sense thanks. For wpv_filter_query, would this replace the entire query or can I specifically add the checkbox functionality and let everything else stay as it is?

#523112

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Hi Joff

That depends on your coding skills.

I recommend you add the filter to your query and simply dump the $view_arguments to your debug log to see what the query currently looks like.

Set up your View with the checkbox filter and the equals comparison. That way it should still get rendered on the front end (though it is unlikely to work correctly for the moment).

Add the following to your functions.php file, editing the view id as required.

/**
 * Customise View query parameters
 */
add_filter( 'wpv_filter_query', 'custom_filter_query', 101, 3);
function custom_filter_query( $views_args, $view_settings, $view_id ){

	if ( 99 == $view_id ) {

		error_log(print_r($view_args, true));
	}
}

And if you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug.log file in your wp-content directory which you can examine in any text editor.

Now visit the page where you have added your View. As you apply filters you will see how the query arguments are modified in your error log. Here Views is effectively just a friendly wrapper for the native WordPress WP_Query class, and you can see what the options are for the arguments here: https://codex.wordpress.org/Class_Reference/WP_Query

If it were just this custom field it would be easy enough to set the conditions for the meta query parameters, but if you have several custom field filters then you will need to incorporate your custom query rather than replace it.

See how you get on and let me know if you get stuck on something specific.

#523149

Thanks Nigel, I'll give this a go. I'm familiar with WP hooks so this shouldn't be an issue. I'll keep the thread as unresolved for now until I've made an attempt on the task.

#523332

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Great, it sounds like you know what you are doing.

Keep me posted.

#524176

Hi Nigel,

Re. your first reply you mentioned a screenshot example for max employees, but I don't see that. Are you able to attach it please?

#524187

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Screen Shot 2017-05-15 at 11.02.38.png

Sorry I missed uploading that, doing so now...

#524303

Thanks. I wasn't seeing the comparison function field on mine for some reason, but now it's there all of a sudden!

#524312

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

The comparison field you need to scroll down to see it (on my screenshot I changed the CSS so that the whole dialog is visible, so it is easy to miss).

You need anymore help..?

#524961

Hi Nigel,

I've now got a function hooking into wpv_filter_query and I can manipulate the arguments but I can't get a Views filter checkbox added to the Filter Editor in order to determine whether to apply my custom query.

If I add a checkbox to a non-Types field (I'm using ACF), the View outputs the checkbox as "Empty field values or incorrect field defined.". If I create a Types Post Field of type Checkbox, I can create a new filter based on it but nothing is output on the front end - I was expecting to see a checkbox that I could have appropriated as a flag for my custom query.

Any ideas?

#525209

Hi Nigel,

I think I was over-complicating a solution. I think the wpv_filter_query hook is the correct approach but I've been able to get the results I want from simply using JS to populate a hidden Views Filter textbox which is then searched on.

Thanks for your help!

#525210

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Hi Joff

I was just working on this at the moment. When I initially suggested using the Views API I was able to get the field to render a checkbox on the front end, but for the life of me I can't now find the combination of settings where I managed to do so and was about to abandon that approach to think of another solution.

So I'm glad you beat me to the punch. Thanks for letting me know.

#525239

Not to worry, I still think there would be mileage in having the ability to use checkboxes in this way with Views so maybe one to escalate to a potential feature in the future?

Thanks again for your time.

Le forum ‘Types Community Support’ est fermé à de nouveaux sujets et réponses.

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