Skip to content

Instantly share code, notes, and snippets.

@marrisonlab
Created October 31, 2025 16:01
Show Gist options
  • Select an option

  • Save marrisonlab/7683a636349585f5d8848bcda25559c3 to your computer and use it in GitHub Desktop.

Select an option

Save marrisonlab/7683a636349585f5d8848bcda25559c3 to your computer and use it in GitHub Desktop.
Filter jetengine grid by the current user’s role
add_filter( 'jet-engine/query-builder/query/items', function( $items, $query ) {
if ( empty( $items ) || ! is_array( $items ) ) {
return $items;
}
$taxonomy = 'authorizations';
$user = wp_get_current_user();
$user_role = $user->roles ? $user->roles[0] : 'guest';
$filtered = [];
foreach ( $items as $item ) {
$post_id = 0;
if ( is_object( $item ) && property_exists( $item, 'ID' ) ) {
$post_id = (int) $item->ID;
} elseif ( is_array( $item ) && isset( $item['ID'] ) ) {
$post_id = (int) $item['ID'];
} else {
$post_id = (int) ( $item->id ?? $item->ID ?? 0 );
}
if ( ! $post_id ) {
continue;
}
$terms = wp_get_post_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
if ( in_array( $user_role, $terms, true ) ) {
$filtered[] = $item;
}
}
return $filtered;
}, 10, 2 );
add_filter( 'jet-engine/listings/allowed-posts', function ( $posts ) {
if ( empty( $posts ) || ! is_array( $posts ) ) {
return $posts;
}
$taxonomy = 'authorizations';
$user = wp_get_current_user();
$user_role = $user->roles ? $user->roles[0] : 'guest';
$filtered = [];
foreach ( $posts as $post ) {
$terms = wp_get_post_terms( $post->ID, $taxonomy, [ 'fields' => 'slugs' ] );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
if ( in_array( $user_role, $terms, true ) ) {
$filtered[] = $post;
}
}
return $filtered;
}, 20 );
@marrisonlab
Copy link
Author

Create a custom taxonomy (e.g. authorizations)
Add terms inside that taxonomy for each user role you want to authorize.
The slug of each term must match the WordPress user role (e.g. subscriber, contributor, etc.)

@marrisonlab
Copy link
Author

Add a check $query->query_id == 101 in the query/items filter.
Add a check $listing->get_id() == 101 in the allowed-posts filter.
for limit snippet to grid with id 101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment