Skip Navigation

[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.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-520693

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

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.

This topic contains 9 replies, has 4 voices.

Last updated by nicholasG-2 5 months, 2 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2716041

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.

Is there any documentation that you are following?
Yes: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-520693

What is the link to your site?
hidden link

Thanks for your assistance!

#2716564

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2717728

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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"]

 <div id="form-e19e65caab428d02c88d9170791334e5-77-wrapper" class="form-item form-item-checkbox wpcf-form-item wpcf-form-item-checkbox"><input type="checkbox" class="js-wpv-filter-trigger wpcf-form-checkbox form-checkbox checkbox" name="wpv-wpcf-racial-identity[]" value="ALL" id="form-e19e65caab428d02c88d9170791334e5-77"><label class="wpcf-form-label wpcf-form-checkbox-label" for="form-e19e65caab428d02c88d9170791334e5-77">ALL</label></div>

For example:

 <div id="form-e19e65caab428d02c88d9170791334e5-77-wrapper" class="form-item form-item-checkbox wpcf-form-item wpcf-form-item-checkbox"><input type="checkbox" class="js-wpv-filter-trigger wpcf-form-checkbox form-checkbox checkbox" name="wpv-wpcf-racial-identity[]" value="ALL" id="form-e19e65caab428d02c88d9170791334e5-77"><label class="wpcf-form-label wpcf-form-checkbox-label" for="form-e19e65caab428d02c88d9170791334e5-77">ALL</label></div>
[wpv-control-postmeta field="wpcf-racial-identity" default_label='ALL' url_param="wpv-wpcf-racial-identity" output="legacy"]

But then you will have to adjust the filter for option "ALL" using the "wpv_filter_query" hook:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2718153

Hi Minesh,

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.

Thanks,
Nick

#2718478

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me what is your expected result when you select "ALL" option.

When "ALL" option selected - you want to display the posts where all the checkboxes options checked for the post - corrct?

#2718819

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.

Thank you, again.

#2723129

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

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.

#2724525

Hi Christopher,

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.

Thanks,
Nick

#2727291

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Hi Nick

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.

#2727508

Hey Nigel,

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.

Thanks again!

Cheers,
Nick

#2727553

This worked! Thanks, Nigel, Minesh, and Christopher!

Cheers,
Nick