Skip Navigation

[Resolved] How do a check so see if data exist in a database field for the current user

This support ticket is created 5 years, 4 months 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.

Our next available supporter will start replying to tickets in about 1.04 hours from now. Thank you for your understanding.

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 2 voices.

Last updated by troyR-2 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1159065

I just need to use the "wpv conditional" to check if data exists in the database for a particular custom field. If there is NO data, I would like to do something. For example, I have a custom field called "post-title-verified." I want to check for the current user and if the user left any data in "post-title-verified." So far, the following does not work I assume because I need to also limit the condition to the current user:

[wpv-conditional if="( $(wpcf-post-title-verified) eq '' )"] ... [/wpv-conditonal]
AND
[wpv-conditional if="( empty($(wpcf-post-title-verified)) )"]...[/wpv-conditional]

I also tried the following the recommendation from the following https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user as such:

[wpv-conditional if="( '[wpv-user field='post-title-verified']' eq '' )"]...[/wpv-conditional]

I don't care for back-and-forth banter. Just give me your best answer. Thanks.

#1159087

Correction: Since added by Types, I used the following:

[wpv-conditional if="( '[wpv-user field='wpcf-post-title-verified']' eq '' )"]...[/wpv-conditional]

It still does not work as it reads the current user's field as empty even when it's not.

#1159647

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Troy,

Thank you for contacting us and I'll be happy to assist.

The value of "[wpv-user field='wpcf-post-title-verified']" shortcode ( https://toolset.com/documentation/user-guides/views-shortcodes/#vf-154505 ), varies based on whether it is being used inside a loop of users in a view on not.

Inside a user loop, it will get the value of user meta field from the current user in the loop and not the currently logged-in user.

To make sure to always get the value of a user field from the currently logged-in user (inside or outside the loop), you can use the shortcode:


[types usermeta='post-title-verified' current_user='true'][/types]

Please test this shortcode alone first to confirm it is displaying the user field value from the user that you need. Once this is confirmed, you can use it in the conditional shortcode like this:


[wpv-conditional if="( '[types usermeta='post-title-verified' current_user='true'][/types]' eq '' )"]
// value is empty
[/wpv-conditional]

[wpv-conditional if="( '[types usermeta='post-title-verified' current_user='true'][/types]' ne '' )"]
// value is not empty
[/wpv-conditional]

I hope this helps and please let me know how it goes.

regards,
Waqar

#1159669

I applied your solution that applies a shortcode that should ALWAYS get the current user. However, it does not work. Please review my video and comment:

hidden link

#1159744

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Troy,

Thank you for sharing the video and I apologize for the confusion earlier.

Since you mentioned you've tried the shortcode "wpv-user", it led me to believe that the "post-title-verified" field was a user custom field ( ref: https://toolset.com/documentation/user-guides/user-fields/ ) and not a post custom field ( ref: https://toolset.com/documentation/user-guides/using-custom-fields/#how-to-add-custom-fields-to-content ).

From the video, it is now clear that the "post-title-verified" field is a post custom field which is saved with each of your "Religious Order Applications" custom post type.

For this reason, the value from it won't be available on "My Account" page directly, which I'll assume is a regular page.

Is there any special reason, you're not storing "post-title-verified" value as a user field? If you could share more details about your required workflow, I'll be in a better position to guide you accordingly.

It would also help if you could share the temporary admin login details so that I can look in the admin area to see how custom post type and fields are configured.

Your next reply will be private, which means only you and our support team will have access to it.

Important note: Please make a complete backup copy of your website, before sharing the access details.

regards,
Waqar

#1160573

I can't tell if you received my new instructions.

#1160579

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Troy,

Thank you for sharing the details and the login access.

I can confirm that the logins work and will follow up on this shortly.

Thank you for your patience.

regards,
Waqar

#1160667

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Troy,

Thank you for waiting.

Based on what you've shared, my understanding is that you need to hide the "Create Religious Order Form (Admin start here for client setup.)" link from "My Account" page, from a user who has once submitted that form ( "Create content - Religious Order Applications" ).

If this is correct, the most efficient way to achieve this would be to register a new single line user custom field (e.g. "Has Submitted ROA Form"), like you've registered a "Mobile Phone" field.

Next, you can add a custom code to your active theme's "functions.php" file, that will automatically update this new user field with a value "yes" when a user submits the form "Create content - Religious Order Applications".
( https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )


add_action('cred_save_data', 'check_submission_roa_form',10,2);
function check_submission_roa_form($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==6683)
	{
		//get the current user's ID
		$user_id = get_current_user_id();
		$key = 'wpcf-user-field';
		$value = 'yes';

		// update field value with yes
		update_user_meta($user_id, $key, $value);
	}
}

Note: Please replace "user-field-slug" with the actual slug of your newly created user field.

After that, you'll be able to use the conditional shortcode to show or hide specific content from users, based on whether they have already submitted that form or not.
( https://toolset.com/forums/topic/how-do-a-check-so-see-if-data-exist-in-a-database-field-for-the-current-user/#post-1159647 )

Let me know how it goes and if you have any questions/concerns around this approach.

regards,
Waqar

#1163449
php-my-admin2.jpg
New User Field.jpg
Function for New User Field Update.jpg

Hi Waqr,

I went ahead and set up the code the way you instructed. update_user_meta does not seem to be doing anything.

Also, you haven't explained how I can build my conditional from the new User field that will now hold the "yes" value.

1) Can you please log in and check for any issues with the logic. I'm assuming wp_usermeta table holds the new value. But, I'm not seeing it updated. I have some snapshots for you to look at.

2) Can you also explain how I can build a conditional to test my new User field that will hold the "yes" value.

Thanks.

#1165505

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Troy,

Thank you for waiting and sorry I couldn't follow up on this sooner.

During testing, I noticed that the custom code is working as expected.

In the new user field's settings, you have set the default value to "No", which is fine.
( screenshot 1: hidden link & screenshot 2: hidden link )

In the custom code, you've set the new automatically updated value to "no", though it would make more sense to change it to "Yes".
( screenshot: hidden link )

When I submitted the form "Religious Order Application" ( at hidden link ), the value of my logged-in user's field changed from "No" to "no", confirming that the code is actually working.
( screenshot: hidden link )

Once you've updated the value "no" to "Yes" in the custom code, you'll be able to use this field in conditions like suggested in my first message:
(I've tested and confirmed that this is working too)


[wpv-conditional if="( '[types usermeta='submitted-religious-order-application-form' current_user='true'][/types]' eq 'Yes' )"]
// Form is submitted
[/wpv-conditional]

[wpv-conditional if="( '[types usermeta='submitted-religious-order-application-form' current_user='true'][/types]' ne 'Yes' )"]
// Form is not submitted
[/wpv-conditional]

I hope this makes it more clear.

regards,
Waqar

#1167219

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.