It is possible, I list images of the current user and administrator, below is the code I am using.
It currently works normally for just the current user to list their images, but I wish the admin images could also be listed.
In other words, listing the images of the current user and the administrator, is it possible?
// Page media list only store images
add_filter( 'posts_where', 'attachments_wpquery_where' );
function attachments_wpquery_where( $where ) {
global $current_user;
if ( is_user_logged_in() && !current_user_can( 'administrator' ) ) {
// we spreken over een ingelogde user
if( isset( $_POST['action'] ) ){
// library query
if( $_POST['action'] == 'query-attachments' ) {
$where .= ' AND post_author='.$current_user->data->ID;
}
}
}
return $where;
}
CodePudding user response:
You can extend your ‘where’ clause. Here I assume that your administrator user ID is 5. You can get that dynamically but for simplicity you can update the below code:
$where .= ' AND post_author='.$current_user->data->ID .’ OR post_author = 5’;
CodePudding user response:
I managed using this code, the conditional makes all the difference.
// Page media list only store images
add_action( 'pre_get_posts','users_own_attachments' );
function users_own_attachments( $wp_query_obj ) {
global $current_user, $pagenow;
if ( current_user_can( 'administrator' ) )
return;
if ( !is_a( $current_user, 'WP_User' ) )
return;
if ( ( 'edit.php' != $pagenow ) && ( 'upload.php' != $pagenow ) && ( ( 'admin-ajax.php' != $pagenow ) || ( $_REQUEST['action'] != 'query-attachments' ) ) )
return;
$author__in = array( $current_user->id, 1 );
$wp_query_obj->set('author__in', $author__in );
return;
}