Skip Navigation

[Resolved] Conditional IF NULL THEN

This support ticket is created 2 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 10 replies, has 3 voices.

Last updated by simonM-5 2 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2486363
Screenshot 2022-10-31 at 17.44.12.png

Hi Support

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

Thanks and regards
Simon

#2486709

Nigel
Supporter

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

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

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

https://toolset.com/course-lesson/using-toolset-conditional-block/#checking-for-values-from-custom-functions

#2487657

Hi Nigel

I found this post on the forum: https://toolset.com/forums/topic/check-if-field-is-not-empty/

It seems to work for our field in the negative:

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

Thanks and best regards
Simon

#2488375

Hi Simon,

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.

regards,
Waqar

#2488431
Screenshot 2022-11-03 at 08.49.00.png
Screenshot 2022-11-03 at 08.48.19.png

Hi Waqar

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.

Thanks and kind regards
Simon

#2489389

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.

I hope this makes sense.

#2489441
Screenshot 2022-11-04 at 14.37.31.png

Hi Waqar

Hmm, I changed the codes as per your suggestions:

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

I added another screenshot

Kind regards
Simon

#2491205

I'm sorry and I should've included the example format in my last message.

This is the complete shortcode that we're using to get the link to the post from the "related-ad" field:


[wpv-post-link item='[types field='related-ad'][/types]']

So in the conditional statement too, the same shortcode will be needed:


[wpv-conditional if="( '[wpv-post-link item='[types field='related-ad'][/types]']' eq '' )"]
This field is empty!
[/wpv-conditional]

You can see it working in the view "Messages - Outbox".

#2491271
Screenshot 2022-11-07 at 14.06.47.png

Hi Waqar

Once again, thank you so much!

I have added similar code for the TO field:

[wpv-conditional if="( '[wpv-user field='user_firstname' id='[types field='message-to'][/types]']' eq '' ) AND ( '[wpv-user field='user_lastname' id='[types field='message-to'][/types]']' eq '' )"]User no longer available[/wpv-conditional]

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!

Kind regards
Simon

#2491823

So glad my suggestions proved useful, Simon!

The syntax of your conditional statement looks good.

Please let me know if you have any follow-up questions. For a new question or concern, you're welcome to start a new ticket.

#2492885

My issue is resolved now. Thank you!