Skip Navigation

[Resolved] Conditional code using a repeating field (not a field group)

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Minesh 1 year, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2650059

Tell us what you are trying to do?
Check if any of the values in a repeating custom field contains the current users username

Is there any documentation that you are following?
Everything I can find in support and online

I feel there is probably a simple way to do this - but I can't find it.

My basic conditional code will only match the first value of the repeating field.

I need to condition to look through all of the values in the repeating field (not just the first) an see if there is a match.

[wpv-conditional if="( 'username' eq $(wpcf-admins_username) )" debug="true"]TEST A[/wpv-conditional]
This just evaluates to the first value in the repeating field

I can use the types field - but that brings through a string for the evaluation so that doesn't work either:
[wpv-conditional if="( 'username' eq '[types field='admins_username'][/types]' )" debug="true"]TEST B[/wpv-conditional]

Is there any guidance on how to evaluate conditions against the repeating custom field?

Thanks

#2650099

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know that the custom field you created "admins_username" that is a repeating field that holds the username - is that post custom field or user custom field.

Also, can you please share screenshot how the values stored in the "admins_username" field?

Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2650103
Screenshot 2023-10-04 at 11.48.50.png

I would like to know that the custom field you created "admins_username" that is a repeating field that holds the username - is that post custom field or user custom field.
This is a post custom field - the proper field name is pmh_admins_username (I just abbreviated it for the example I sent)

If I display that field as follows:
[types field='pmh_admins_username'][/types]
I get: karlequi kequi

For this conditional:
[wpv-conditional if="( 'kequi' eq $(wpcf-pmh_admins_username))" debug="true"]user and pmh admns match kequi[/wpv-conditional]

This is the debug report - as you can see the keyword $(wpcf-pmh_admins_username) just converts to the first value in the repeating field:

####################
wpv-conditional attributes
####################
Array
(
[if] => ( 'kequi' = $(wpcf-pmh_admins_username))
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'kequi' = $(wpcf-pmh_admins_username))
--------------------
--------------------
Converted expression: ( 'kequi' = 'karlequi')
--------------------

If I use the types short code:
[wpv-conditional if="( 'kequi' eq '[types field='pmh_admins_username'][/types]' )" debug="true"]user and pmh admns match kequi[/wpv-conditional]

It brings both values through as a string - and there is no match:

####################
wpv-conditional attributes
####################
Array
(
[if] => ( 'kequi' = 'karlequi kequi' )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'kequi' = 'karlequi kequi' )
--------------------
After replacing 1 general variables and comparing strings: ( 'kequi' = 'karlequi kequi' )
Comparing kequi to karlequi kequi

Thank you

#2650125

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I suggest in this case its better to use custom shortcode.

For example - Add the following custom shortcode to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

function func_check_username() {
 
 global $current_user;
 global $post;

	$all_usernames = types_render_field("pmh_admins_username",array('output'=>'raw','separator'=>","));
	if(!empty($all_usernames)) {
		$all_usernames = explode(",",$all_usernames);
		if(in_array($current_user->user_login,$all_usernames)) {
			return 1;
		}
	}
	return 0;
}
 
add_shortcode( 'check_username', 'func_check_username' );

- Where I've checked for the current user dynamically.

Then register/add the shortcode name (without quotes) 'check_username' under Toolset => Settings => Front-end Content => Third-party shortcode arguments section.

Then you can add the conditional statement as given under

[wpv-conditional if="( '[check_username]' eq '1')" debug="true"]
user and pmh admns match kequi
[/wpv-conditional]

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/

#2650177

Thank you - that is working now for that specific case.

Is it possible to extend this to a parent in a relationship? (Toolset relationship)

We have companies with multiple related posts in a directory type structure.
Companies and posts are connected via a Toolset relationship (one to many).
When a user is editing a post - I need to do the username check on the parent company asset (not the post they are looking at)

I know I can do this sort of thing to access the custom field in the parent:
[types field='pmh_admins_username' separator=', ' item='@pmhub-profile-pmhub-article.parent'][/types]

But how do I translate that into the shortcode function you provided?

Thanks

#2650323

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following updated shortcode:

function func_check_username() {
  
 global $current_user;
 global $post;
 
    $all_usernames = types_render_field("pmh_admins_username",array('output'=>'raw','separator'=>"," , 'item'=>'@pmhub-profile-pmhub-article.parent'));
    if(!empty($all_usernames)) {
        $all_usernames = explode(",",$all_usernames);
        if(in_array($current_user->user_login,$all_usernames)) {
            return 1;
        }
    }
    return 0;
}
  
add_shortcode( 'check_username', 'func_check_username' );

As you can see I've added the item attribute to the types_render_field() function.

#2650601

Hi,

Frustratingly I can't get that types_render_field to work on the parent.

Works fine as a shortcode in the content template itself:
[types field='pmh_admins_username' separator=', ' item='@pmhub-profile-pmhub-article.parent'][/types]
This returns the 3 field values as expected from the parent page

But this in the shortcode returns nothing:
types_render_field("pmh_admins_username",array('output'=>'raw','separator'=>',','item'=>'@pmhub-profile-pmhub-article.parent'));

The types_render_field part is working (I can return any fields form the current post using this)
EG: types_render_field("permalink-test", array())

But I can not return any fields from the parent asset - even those that are not repeating fields.
EG: types_render_field("pmh_awards",array('item'=>'@pmhub-profile-pmhub-article.parent'))

Which seems odd as the item tag works fine in the shortcode.

Do you have any further suggestions?

#2650603

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL and admin access details and let me check whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2650607

Thanks Minesh.
You might as well login to see everything (as the site is password protected also)

I've set you up a temporary admin login without password:

hidden link

The content template I am testing all of the shortcodes and conditionals:
hidden link

The article I am testing the content template on:
hidden link

The snippet where I am storing the php:
hidden link
NB: I have added a second short code (and added it to Toolset just to test what is being returned from the types_render_field)

Let me know if there is anything else you need.

The site was backed up last night.

#2650619

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the shortcode as given under:

function func_check_username_article() {
  
 global $current_user;
 global $post;
    
	$parent_id = toolset_get_related_post($post->ID,'pmhub-profile-pmhub-article');
	
    $all_usernames = types_render_field("pmh_admins_username",array('output'=>'raw','separator'=>',','item'=>$parent_id));
	
	
    if(!empty($all_usernames)) {
        $all_usernames = explode(",",$all_usernames);
        if(in_array($current_user->user_login,$all_usernames)) {
            return 1;
        }
    }
    return 0;
}
  
add_shortcode( 'check_username_article', 'func_check_username_article' );

Can you please check now and confirm it works as expected now.

#2650625

Fantastic - thanks Minesh,
I've got a good example there of how to use this going forward also.
Much appreciated.