Skip Navigation

[Resolved] custom values for wpv_filter_query

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

Problem:

Setup a checkbox field in Views custom search form.

I need to add a checkbox (not associated with a types custom field) to a view search form, to use the _GET value in a custom wpv_filter_query, something like "if checkbox is checked then return a list of IDs else return another list of IDs").

Solution:

It needs to be added into white list of URL parameter, please try these:

https://toolset.com/forums/topic/custom-values-for-wpv_filter_query/#post-1138008

Relevant Documentation:

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

Our next available supporter will start replying to tickets in about 8.73 hours from now. 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by emanuelaB 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1136194

I need to add a checkbox (not associated with a types custom field) to a view search form, to use the _GET value in a custom wpv_filter_query, something like "if checkbox is checked then return a list of IDs else return another list of IDs").

<input type="checkbox" id="mycheckbox" class="checkbox" name="mycheckbox" value="1">

I've tried in several ways, even with select and radio and also using javascript: first submit is ok, but if I re-submit the search I get two values for the checkbox in query string, for example

first run

...view-name/?wpv_view_count=64261&mycheckbox=1&wpv_post_search=something&wpv_filter_submit=Send

second run

...view-name/?&mycheckbox=0&wpv_view_count=64261&mycheckbox=1&wpv_post_search=something-else&wpv_filter_submit=Send

is it possible to do it in some other way?

Thank you in advance

#1136595

Hello,

I suggest you setup a custom checkbox field "mycheckbox", option "Value to store" inputs value 1.

Then you will setup the "mycheckbox" field in custom search form, like this:

[wpv-control-postmeta field="wpcf-mycheckbox" type="checkbox" url_param="mycheckbox"]

And in your case, don't need to setup the filter for "mycheckbox" field, it won't take effect on the search result.

#1137534

Hi
thanks for your reply, but I'm afraid there'se still something wrong.

I've created a custom checkbox field "mycheckbox", value to store = 1, and I've inserted the wpv-control-postmeta in view, without using it in query filters

first search, mycheckbox not checked, query string

wpv_view_count=64261&wpv_post_search=something&wpv_filter_submit=Send

second search, mycheckbox checked, query string

wpv_view_count=64261&wpv_post_search=&mycheckbox=1&wpv_filter_submit=Send

third (and fourth, and so on) search, mycheckbox UNchecked, query string

mycheckbox=1&wpv_view_count=64261&wpv_post_search=&wpv_filter_submit=Invia

In other word, if I check mycheckbox, then I can no more uncheck that.

Any hint?

Thanks in advance.

#1138008

You are right, it needs to be added into white list of URL parameter, please try these:
1) Setup a custom checkbox field "mycheckbox"

2) setup the "mycheckbox" field in custom search form, like this:

[wpv-control-postmeta field="wpcf-mycheckbox" type="checkbox" url_param="mycheckbox"]

don't need to setup the filter for "mycheckbox" field

3) Add below PHP codes into your theme file "functions.php":

add_filter('wpv_filter_register_url_parameters_for_posts', 'add_extra_url_param_func', 10, 2);
function add_extra_url_param_func($attributes, $view_settings){
     
    if($view_settings['view_id'] == 164){
        $attributes[] = array(
            'query_type'=> 'posts',
            'filter_type'=> 'post_custom_field_mycheckbox',
            'value'=> 'custom_field_value',
            'attribute'=> 'mycheckbox',
            'expected'=> 'number',
        );
         
    }
    return $attributes;
}

And test again

#1139510

Thank you very much, it works!
Best regards