Skip to content

Instantly share code, notes, and snippets.

@luistinygod
Last active October 29, 2021 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save luistinygod/ec515f50debcfcacee81 to your computer and use it in GitHub Desktop.
Save luistinygod/ec515f50debcfcacee81 to your computer and use it in GitHub Desktop.
GravityView extend the Advanced Filter to a certain users' role
<?php
/**
* When using the Advanced Filter extension to filter entries to show just the ones created by the user logged-in
* BUT remove the restriction to a certain role of users ( administrators or editors )
*
* Replace the following to your own values:
* - REPLACE_BY_VIEW_ID, use the view id where this logic applies
* - REPLACE_BY_THE_ROLE, use 'administrator' or 'editor' or your own custom role
*
*
* Add this gist to your theme functions.php
*
*/
add_filter( 'gravityview_search_criteria', 'my_gv_custom_role_filter', 110, 1 );
function my_gv_custom_role_filter( $criteria ) {
// check if we are displaying the correct View
if( function_exists('gravityview_get_view_id') && 'REPLACE_BY_VIEW_ID' != gravityview_get_view_id() ) { return $criteria; }
// Check if user is logged-in
if( !is_user_logged_in() ) { return $criteria; }
// Check if user belongs to a certain 'admin' role
$current_user = wp_get_current_user();
if( !in_array( 'REPLACE_BY_THE_ROLE', $current_user->roles ) ) { return $criteria; }
// if we get here, then remove the 'create by' filter from the $criteria
if( !empty( $criteria['search_criteria']['field_filters'] ) && is_array( $criteria['search_criteria']['field_filters'] ) ) {
foreach( $criteria['search_criteria']['field_filters'] as $k => $filter ) {
if( $k !== 'mode' && 'created_by' === $filter['key'] ) {
unset( $criteria['search_criteria']['field_filters'][ $k ] );
}
}
}
return $criteria;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment