[Resolved] Query filter in a view isn't working as expected
This thread is resolved. Here is a description of the problem and solution.
Problem:
The customer created a view with two query filters: (1) the page must be a subpage of the page on which the view resides, and (2) the custom field "Card Exclude" must not have the value "1." Despite having four subpages, with only one marked as excluded, the view displayed no pages. Additionally, the unchecked state of the checkbox field was not saving a value to the database.
Solution:
The issue stemmed from how checkbox field values were stored in the database. If the checkbox was never saved, it remained absent in the database. If checked and then unchecked, it stored a value of 0, while being checked saved a value of 1. The workaround applied involved manually saving the field as true (1) and then unchecking it to store false (0), allowing the query filter to function correctly. Additionally, two other solutions were suggested:
1- Using a conditional statement in the Loop Editor: [wpv-conditional if="( $(wpcf-exclude-card) ne '1' )"].
2- Custom code in the query to handle cases where the field might be NULL or 0.
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.
I've created a view to list pages with two query filter conditions:
+ the page must be a subpage of the page on which the view resides.
+ the value of a custom field on the subpage, "Card Exclude," cannot be "1."
See the attached screenshot "screenshot 2024-10-31 at 11.08.39.png" to see the query filter configuration.
"Card Exclude" is a checkbox. When saved, the value is set to "1."
My test page has 4 subpages. One of the 4 pages has the "Card Exclude" field checked. When I visit the public face of the test page, I expect to see a list of 3 pages. Instead, none appear. When I remove the second part of the query filter, four pages display, so something is clearly wrong with the second part, but I do not know what.
Can you please offer some guidance?
Thank you!
Saul
PS. As an aside, I've configured the checkbox to save "0" to the database when it's unchecked. (See "screenshot 2024-10-31 at 11.09.10.png.") But when I look at the database contents, the value isn't being saved. (As mentioned, when the checkbox is checked, the "1" value *is* being saved.)
I've reviewed the issue, and it appears that the root cause lies in how the checkbox field values are saved in the database. As you've observed, if the checkbox is left unchecked and never saved, it has no entry in the database. However, once it’s checked and then unchecked, it saves a value of 0 when deselected and 1 when selected.
I've gone already and applied a workaround related to this. For those three pages, I manually saved the field with true(1) first, then unchecked it to set the value to false(0). This workaround has updated the database for those entries, and the query filter is now working correctly without the need of the conditional.
In addition, there are other approaches we can explore:
1- Use of a Conditional(currently disabled) – You’ve already implemented this in the Loop Editor, which is an effective approach:
[wpv-conditional if="( $(wpcf-exclude-card) ne '1' )"]
This will prevent displaying posts where the "Card Exclude" field is set to 1.
2- Custom Code in the Query – Alternatively, custom code can dynamically adjust the query for this view to check for cases where the field might be NULL or 0.
Both methods are viable depending on your preference. If the conditional workaround meets your needs for now, I suggest continuing with that.
Please feel free to reach out if you need any further adjustments!
Thanks for your thorough investigation and detailed reporting. It's not going to be feasible for my client to check that box and save the page and uncheck that box and save the page for all my current and future pages, so I'm going to stick with my solution (the conditional in the Loop Editor).