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
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.
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.
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
Thank you very much, it works!
Best regards