We recently had the first few customers in production who, according to EU GDPR privacy regulations, have requested that their data held with us be deleted. This means that we are required by law to delete their user account, and hence their User in WordPress.
Now we notice that since Toolset can no longer find the user IDs associated with some records that NULLs are being displayed for other users who have contacted these other users via Toolset Messaging.
I wondered whether it is possible to use the Toolset conditional to null-check, ie something equivalent to the SQL "CASE WHEN custom_field_xxxx IS NULL THEN 'show this string' ELSE show_the_real_value END", so that we could display "User no longer available" or "Ad is no longer available" in our Messages section (see example screenshot form our dev site).
I tried adding a conditional on line 18 of Loop Editor of Toolset View "Messages - Outbox", but when I call up the conditional in the GUI, it only offers me =, !=, >, <, >= and <= as operators and no "if null" option. I thought perhaps there might be some manual syntax to do this.
In the first instance in the screenshot, we just deleted the Ad created by the user, in the second instance we deleted the user (and hence all related posts belonging to the user). We would love to be able to replace those NULLs with "User no longer available" or "This Ad is no longer available"
Screenshot showing View: Messages - Outbox hidden link
Toolset conditionals allow you to register custom PHP functions that you can test the return value of.
So if you need to test for NULL, you can register a custom function to get the relevant value and compare it to null, and if it is null return a value that you can test with the available operators in the Toolset conditional (such as 1 or 0, or even "NULL" as a string).
[wpv-conditional if="( NOT(empty($(wpcf-related-ad))) )"]
This field is not empty
[/wpv-conditional]
however in the postive (by removing the NOT()), it doesn't. Does PHP/WordPress store something else in the field when no valid foreign key can be resolved?
1) Is this no longer an acceptable way of checking to see if a field is empty or not?
2) If not, do you have an example of the custom function to check the value of our custom field wpcf-related-ad which I could adapt?
On my test website, all these 3 ways of checking for the empty value, worked as expected.
( this test also covered cases where the custom field record was either not set or set without any value )
[wpv-conditional if="( NOT(empty($(wpcf-related-ad))) )"]
This field is not empty 1
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-related-ad) ne '' )"]
This field is not empty 2
[/wpv-conditional]
[wpv-conditional if="( '[types field='related-ad'][/types]' ne '' )"]
This field is not empty 3
[/wpv-conditional]
In case you're still not getting the expected results, please share the exact testing steps, along with the user access details, that you're testing this with.
Thanks for those examples. I concur, the negative versions you provided all show something in the field, even when it is null (see screenshot).
However, we want the positive versions to work. I converted all 3 versions to positive like so and added them in Lines 19-29 of the View "Messages - Outbox".
[wpv-conditional if="( empty($(wpcf-related-ad)) )"]
This field is not empty 1
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-related-ad) eq '' )"]
This field is not empty 2
[/wpv-conditional]
[wpv-conditional if="( '[types field='related-ad'][/types]' eq '' )"]
This field is not empty 3
[/wpv-conditional]
however when I run them on the front end using test user {removed} (please delete this login once you have noted it), if the field is empty the texts are not being displayed. So it would seem to me that there must be "something" being stored in the fields if nothing is resolved from the foreign key.
Thank you for sharing these details and I think I've figured out, what is causing this confusion.
If you'll just print the value of the "related-ad" field in the view ( [types field='related-ad'][/types] ), you'll see that the stored post IDs are still saved in those custom fields (so they are not empty).
However, the actual posts associated with those saved IDs have been deleted, so when you try to show their post link ( [wpv-post-link item="[types field='related-ad'][/types]"] ), it returns empty, which leads you to believe that the "related-ad" field is empty, when it's not.
So in this case the conditional statement should be using the 'wpv-post-link' shortcode's output and not the output of the "related-ad" field.
[wpv-conditional if="( empty($(wpv-post-link)) )"]
This field is empty 1
[/wpv-conditional]
[wpv-conditional if="( $(wpv-post-link) eq '' )"]
This field is empty 2
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-link]' eq '' )"]
This field is empty 3
[/wpv-conditional]
1) Only the first two show something, which I suspect might just be a syntax issue in the 3rd case...
2) All the fields are showing the message, whereas I would expect the conditional to only show up those texts "This field is empty" in the first two rows of the resulting table, since the remainder of the rows in the table HAVE a non-empty, valid wpv-post-link.
Can you please confirm that the syntax is correct? It seems to work according to my screenshot.
So I will implement something similar on the View "Messages - Inbox" and do a round of testing with my colleague, but once again, it looks like you have helped us immensely with subtle but very valuable code!