Skip Navigation

[Resolved] Setting up a Condition in a View

This support ticket is created 2 years, 8 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.

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 3 replies, has 2 voices.

Last updated by j.-michaelA 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2511843

Tell us what you are trying to do?
I am trying to filter a toolset View to only return custom "Students" post type records created in Toolset, where the Student post field "instructor-user-login" equals the currently logged in user's "username" (user_login) field. I see that you can insert a Conditional block into a View to filter display results. However, I'm having trouble figuring out how to format the condition to limit the post types in the View to only display "instructor-user-login" fields that match the currently logged in User's "username" field. How should I format the View so it will only return records that match this condition?

Is there any documentation that you are following?
Display Content Conditionally (https://toolset.com/course-lesson/using-toolset-conditional-block/)

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#2512253

Hi,

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

There is no direct conditional evaluation available in the conditional block for this, so it will be better to apply this filtering at the view's query level instead, using the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:


add_filter( 'wpv_filter_query', 'wpv_filter_query_custom_func', 1000 , 3 );
function wpv_filter_query_custom_func( $query_args, $view_settings ) {
	// process if specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		// get current user login name
		$current_user = wp_get_current_user();
		$current_user_login = $current_user->user_login;
		$target_custom_field = 'instructor-user-login';

		if(!empty($current_user_login)) {
			$query_args['meta_query'] = array(
				'relation' => 'AND',
				'date_field' => array(
					'key' => 'wpcf-'.$target_custom_field,
					'value' => $current_user_login,
					'type' => 'CHAR',
					'compare' => '=',
				), 
			);
		}

	}
	return $query_args;
}

Note: You'll replace "12345" with your actual view's ID and the 'instructor-user-login', with the actual slug of your target custom field.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2514261

Please do not close out this request for a couple more days. We have been out with Covid for a few days and haven't yet tried to implement your solution. We will be working on it tomorrow and should be done sometime on Wednesday.

I will get back with you then to let you know if we were successful.

Thanks for your patience
Mike

#2515011

Waqar,

Thanks so much for your help! My issue is now resolved.

Mike