Exclude Administrator Role from search filter

I would like to use this filter to create a list visible only to the logged in user. I need to exclude the Administrator role from the filter though. If the administrator is logged in he can see all entries. How should I change this filter?

//add_filter( ‘gform_search_criteria_entry_list’, ‘override_search_criteria’ );
//function override_search_criteria( $search_criteria ) {
// $search_criteria[‘field_filters’] = array( ‘key’ => ‘created_by’, ‘operator’ => ‘is’, ‘value’ => get_current_user_id() );

//return $search_criteria;
//}

Could you try this code and let me know if that works?

add_filter( 'gform_search_criteria_entry_list', 'override_search_criteria' );

function override_search_criteria( $search_criteria ) {
    if ( current_user_can( 'administrator' ) ) {
        return $search_criteria;
    }
    $search_criteria['field_filters'] = array(
        'key' => 'created_by',
        'operator' => 'is',
        'value' => get_current_user_id()
    );
    return $search_criteria;
}

Hi Faisal,

i tried to add the code. Login with admin role it works, but login with another user i see the follow error of illegal string offset:

Hi Alberto,
Please try the following code and let me know how that goes. It should fix the error. :smile:

add_filter( 'gform_search_criteria_entry_list', 'override_search_criteria' );

function override_search_criteria( $search_criteria ) {
    if ( ! is_array( $search_criteria ) ) {
        $search_criteria = array();
    }

    if ( current_user_can( 'administrator' ) ) {
        return $search_criteria;
    }

    $search_criteria['field_filters'] = array(
        'key' => 'created_by',
        'operator' => 'is',
        'value' => get_current_user_id()
    );

    return $search_criteria;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.