[Resolved] default_label is not working with the [wpv-control-postmeta] shortcode.
This thread is resolved. Here is a description of the problem and solution.
Problem:
I am trying to use the default_label attribute with the [wpv-control-postmeta] shortcode to display a default "All" option for a checkbox custom field. Despite following the documentation, the default_label attribute isn't functioning as expected, and adding the option through jQuery is proving complex.
Solution:
Checkbox fields do not natively support a "select all" option via the default_label attribute. Instead, add a custom checkbox labeled "All" manually and use the wpv_filter_query hook to modify the query when this option is selected. Check the $_POST object to identify when "All" is selected and update the query args accordingly.
Tell us what you are trying to do?
Use the 'default_label' attribute with the [wpv-control-postmeta] shortcode. Here's my existing shortcode: [wpv-control-postmeta field="wpcf-racial-identity" default_label="All people of color" url_param="wpv-wpcf-racial-identity" output="legacy"]. Adding the default_label attribute does nothing, and changing the View settings doesn't seem to help, either. I've looked through all docs and examples I could find, but I can't figure out why this isn't working.
I've written some jQuery that adds the item, but it's turning into quite a bit of work to add this via jQuery and then try and filter queries with WP hooks to properly do this, when the 'default_label' option should be all I need.
Hello. Thank you for contacting the Toolset support.
Can you please share details for what custom search filter field you want to set the default label and share admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I can see that you have checkboxes custom field. Checkboxes field type is a special field type whose value stored as serialize array into database.
I checked and there is no default option available for checkboxes field.
You can add the ALL option by adding the following code just before the filter shortcode:
[wpv-control-postmeta field="wpcf-racial-identity" default_label='ALL' url_param="wpv-wpcf-racial-identity" output="legacy"]
Thanks for the update. I tried this, and while the option is added now, when I choose that selection, it's not added to the query, and I can't intercept it using wpv_filter_query. I can verify the filter fires, and when I select a normal option from the [wpv-control-postmeta] shortcodes, they're in the query args, but this manual <div> doesn't seem to be making it into the query.
Yes, I think you have it. With the 'ALL' option selected, I'd like posts with any of this filter's terms checked. In other words, it should be as though the filter has each item checked.
Minesh is on vacation and I will answer back. I checked the page and it seems that clicking the All option shows the whole items as it causes the pagination to go around 102 pages.
Did you manage to fix the issue?
From what I see the value of the ALL checkbox is set to 0 in the HTML code.
Thanks for taking over. No, the issue isn't fixed. With the setup I'd started working on with Minesh, I can't get `wpv_filter_query` to "see" the ALL checkbox being selected. In the logs, when I select ALL, it's as though I've selected nothing from the filter -- it's not present in the $query_args function arg.
sorry to bounce you around supporters, Christopher is off sick.
Let me step in here so you don't have to wait any longer.
From reading the thread it seems there is perhaps a misconception.
The checkboxes field doesn't have an option to select all which is what you appear to want.
Minesh proposes essentially adding a fake input with the label "All", the intention being that you will use the wpv_filter_query hook to intercept this and update the query args so that all options are checked.
You won't find the "All" option set in the query args: you have to set all of the options in the query args.
You would check the global $_POST object to see if the All option has been specified, and then update the query_args by setting all of the options.
To see what is required, I would suggest dumping the $_POST object to the debug.log in your code and submit the filter with the All option checked: you can see how it appears in the $_POST object to know what to look for.
Then, try submitting the filter again, this time with all of the individual checkboxes set. Dump $query_args to the debug.log to see how that is supposed to look.
You should be able to combine the two such that when the All checkbox is checked you can update the query args as if all of the individual checkboxes were checked.
If you get stuck let me know and I'll take a closer look.
Thanks so much for jumping in. This clarifies things a ton, much appreciated. From Minesh's answer, I gleaned that the selection would be in the grouped query_args, but altering the query using $_POST is exactly what I was missing. I'll give this a try.