Skip Navigation

[Resolved] Complex conditionals

This thread is resolved. Here is a description of the problem and solution.

Problem:
Add a repeating custom user field with PHP codes.
Solution:
Use wordpress function update_user_meta, like this
add_user_meta($current_user->ID, 'wpcf-user_member-of-team', $meta_value, false);
Relevant Documentation:
http://codex.wordpress.org/Function_Reference/add_user_meta

This support ticket is created 8 years 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/Hong_Kong (GMT+08:00)

This topic contains 31 replies, has 2 voices.

Last updated by pamelaB-2 8 years ago.

Assisted by: Luo Yang.

Author
Posts
#367584

I have a content template which contains data that I want to hide from specific users.

The condition is:

If {user} is author of {child-post} where {child-wpcf-custom-field} = {current-logged-in-user}, condition TRUE.

Is this possible via the built-in condition system, or will it require custom code?

Here's the scenario:
I have a CPT called TEAM.
Users can create children of TEAM called MEMBERS.
Thus, If you are not a MEMBER of TEAM, do not display certain information.

On another note, and to save some time, I wanted to know if I can update user-meta inside a function that is designed to update post-meta.

Here is my code:

add_action('cred_save_data', 'change_post_title_member',10,2);
function change_post_title_member($post_id, $form_data)
{
    //update this with your form's ID
    if ($form_data['id']==538)
    {
        $current_user = wp_get_current_user();
		
		$parent_team_id = get_post_meta($post_id, '_wpcf_belongs_team_id', true);
		$parent_post = get_post($parent_team_id);
		$parent_team = $parent_post->post_title;
		$parent_slug = $parent_post->post_name;
		
		update_post_meta($post_id, 'wpcf-member-of-team', $parent_team);
		update_post_meta($post_id, 'wpcf-parent-post-slug', $parent_slug);
		update_user_meta($current_user, 'wpcf-user_member-of-team', $parent_team);
		
        wp_update_post(array(
			'ID'=>$post_id, 
			'post_title'=>$current_user->user_login)
		);
    }
}

I want the user-meta field user-member-of-team to be updated with post_title when this form is completed. I have added the types user field to the CRED form, but I don't think this is the right way of doing it, because testing of this does not save the field.

#367884

Dear pamela,

Please try modify your PHP codes from:
update_user_meta($current_user, 'wpcf-user_member-of-team', $parent_team);
To:
update_user_meta($current_user->ID, 'wpcf-user_member-of-team', $parent_team);

More help:
http://codex.wordpress.org/Function_Reference/update_user_meta
Parameters
$user_id
(integer) (required) User ID.
Default: None

#367948

Hi Luoy, thank you.

Please see first question in OP:

I have a content template which contains data that I want to hide from specific users.

The condition is:

If {user} is author of {child-post} where {child-wpcf-custom-field} = {current-logged-in-user}, condition TRUE.

Is this possible via the built-in condition system, or will it require custom code?

Here's the scenario:
I have a CPT called TEAM.
Users can create children of TEAM called MEMBERS.
Thus, If you are not a MEMBER of TEAM, do not display certain information.

#367952

Hi Luoy.

I changed the code as you said, but it still does not work.

In this scenario, I am trying to save data to the USER inside a CRED POST form. Is this possible, or can I only update user meta inside a CRED USER form?

#368178

Working against a deadline here... Please could you try to address these issues a little quicker. For instance, reading the entire post the first time could have prevented a day's time wasted.

#368304

Sorry for the misunderstand.
Q1) Is this possible via the built-in condition system, or will it require custom code?
How do you setup this custom field "child-wpcf-custom-field"?
Are you going to use Views shortcode [wpv-conditional] shortcode to display what you want?
Here is a document about "Creating blocks that display conditionally":
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
Section "Using Custom Functions Or Class Methods"
There you can use your custom functions insider [wpv-conditional] shortcode
Hope it help

Q2) In this scenario, I am trying to save data to the USER inside a CRED POST form. Is this possible, or can I only update user meta inside a CRED USER form?
Yes, it is possible within CRED action hook cred_save_data,
As I mentioned above,
https://toolset.com/forums/topic/complex-conditionals/#post-367884
there is a mistake in your PHP codes, the parameter
$user_id
(integer) (required) User ID.

Which is a integer, you will need to use $current_user->ID in function update_user_meta,
And you will need to check if you are using the correct user field slug, as you mentioned in post:
https://toolset.com/forums/topic/complex-conditionals/#post-367584
you are using below PHP codes
update_user_meta($current_user, 'wpcf-user_member-of-team', $parent_team);
the field slug is "wpcf-user_member-of-team"
But you mentioned also:
I want the user-meta field user-member-of-team
which is "user-member-of-team", it is different field slug

since it is a custom PHP codes problem, if you need more assistance for it, please duplicate same problem in a test site, and fill below private detail box with login details, also point out the problem page URL and CRED for URL, and where I can edit your PHP codes,

#368350

If you need to add new custom post fields, please try with wordpress function "add_post_meta":
http://codex.wordpress.org/Function_Reference/add_post_meta
Parameters
$unique
(boolean) (optional) Whether or not you want the key to stay unique. When set to true, the custom field will not be added if the given key already exists among custom fields of the specified post.
Default: false

If you need to add new custom user fields, please try with wordpress function "add_user_meta":
http://codex.wordpress.org/Function_Reference/add_user_meta
$unique
(boolean) (optional) Whether the same key should not be added.
Default: false

#368374

Hi Luoy.

Thank you, that worked perfectly.

If you wouldn't mind, I'd like to ask one other question here instead of a new thread, just to save some time. It is related to the above query.

Now that I am using add_user_meta to collect data every time a child post is created, I need to write a conditional based on it, and could use your help here.

Since there may be multiple instances of this CF, I need the conditional to say:

FOR EACH {custom-field}:
IF {custom-field} = {other-custom-field} THEN
RESULT

Is this possible within the conditional system?

#368383

I probably need to clarify further (post edit doesn't seem to be working today):

If ANY of the instances of {custom-field} = {other-custom-field} THEN
RESULT

#369011

I assume you are going to use Views shortcode [wpv-conditional] to do you mentioned above.
Yes, it is possible within custom functions, see our document:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#custom-functions
Using Custom Functions Or Class Methods

Hope it help

#369034

Hi Luoy.

Thanks.

Are you able to help me with the custom function? I am not a coder 🙁

#369129

Here is an example:
I assume both custom user fields are created with Types plugin, using field slug "custom-field" and "other-custom-field", you are going to compare the values of current logged in user, and you can try this:
1) add below codes into your theme/functions.php:

function my_compare_func($field1, $field2){
	$val1 = get_user_meta(get_current_user_id(), 'wpcf-' . $field1, false);
	$val2 = get_user_meta(get_current_user_id(), 'wpcf-' . $field2, true);
	$res = 2;
	if(in_array($val2, $val1)){
		$res = 1;
	}
	return $res;
}

2) Dashboard-> Views-> Settings-> Compatibility
in option "Functions inside conditional evaluations", add the function name "my_compare_func"

3) use above custom function inside [wpv-conditional] like this:

[wpv-conditional if="(my_compare_func('custom-field', 'other-custom-field') eq 1)"]
...
here display something
...
[/wpv-conditional]
#369156

So I have done something like this:

function is_in_team($field1, $field2){
    $val1 = get_user_meta(get_current_user_id(), 'wpcf-' . $field1, false);
    $val2 = get_post_meta(get_the_ID (), $field2, true);
    $res = 2;
    if(in_array($val2, $val1)){
        $res = 1;
    }
    return $res;
}
[wpv-conditional if="(is_in_team('user-member-of-team', '[wpv-post-title]') eq 1)"]
[wpv-view name="team-members"]
[/wpv-conditional]

This always evaluates as false. Can you see what I have done wrong?

#369191

I think I figured it out, but will test some more first before close thread.

function is_in_team($field1){
    $val1 = get_user_meta(get_current_user_id(), 'wpcf-' . $field1, false);
    $val2 = get_the_title();
    $res = 2;
    if(in_array($val2, $val1)){
        $res = 1;
    }
    return $res;
}

and:

[wpv-conditional if="(is_in_team('user-member-of-team') eq 1)"]
#369199
user-member.JPG

I don't think it's working just yet.

See attached, I have data filled in for 2 different teams, but the conditional evaluates false for the second instance, so I think maybe the function is checking only the first instance.

OR

The apostrophe is causing a problem? Pamela>>'<<s Team.

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