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
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
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
Waqar,
Thanks so much for your help! My issue is now resolved.
Mike