Open nreljed opened 3 years ago
follow
@kaskad88 This will help you a lot and speed up the implementation of this primordial missing option this code seems to reproduce the limiting function we want
add_filter( 'ajax_query_attachments_args', 'filter_query_attachments_args' );
function filter_query_attachments_args( $query ) {
// 1. Only users with access
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error();
}
// 2. No manipulation for admins.
// After all they have access to all images.
if ( current_user_can( 'administrator' ) ) {
return $query;
}
// 3. No images, if the post_id is not provided
if ( ! isset( $_REQUEST['post_id'] ) ) {
wp_send_json_error();
}
// 4. No images, if you are not the post type manager or author
$post = get_post( (int) $_REQUEST['post_id'] );
if ( ! $post instanceof \WP_Post ) {
return $query;
}
// 5. You can also restrict the changes to your custom post type
if ( 'listing' != $post->post_type ) {
// Only filter for our custom post types
return $query;
}
// 6. Allow only post authors to open the uploader
$current_user = wp_get_current_user();
if ( $current_user->ID != $post->post_author ) {
wp_send_json_error();
}
// 7. Filter to display only images
$query['post_mime_type'] = array(
'image/gif',
'image/jpeg',
'image/png',
'image/bmp',
'image/tiff',
'image/x-icon'
);
// 8. Don't show private images
$query['post_status'] = 'inherit';
// 9. Filter to display only the images attached to the post
$query['post_parent'] = $post->ID;
// 10. Filter to display only the user uploaded image
$query['author'] = $current_user->ID;
return $query;
}
✔️tested and working
there may be too many or unnecessary codes but you get the idea
Each part is documented with its number and details.
the only problem ... it takes more than 5 to 7 seconds to display the images
probably because of the nature of the ajax_query_attachments_args
function ?
or some adjustments that you can probably do, to make it compatible 100% with jetengine
these people seem to be talking about the same thing with a different approach. I did not understand how to test it. but i see
ajax_query_attachments_args
again.. so i imagine i will have the same loading time problem...
maybe it will help you better understand
I also tried to find the code of this function in ACF files but not succeeded .. if anyone understands how to find it.
Thank you for sharing it
we must help the dev to understand how to reproduce this function
This would be great... how is it going with this request?
I suggest that we add this "very popular" option from ACF.
this illustration will show you what problem it will solve. especially if the uploads to the posts are unique like real estate.
imagine you are a real estate agent and you want just upload images of a house. in backend if you click on "Choose Media" ths is will show you all your uploads and all other users uploads it's anarchy!!
Library option limit what you can see each agent must only and only
directly upload. [new post] see the images attached to the post. [edit post] insert images or delete some if necessary. [edit post]
one last request .
I suggest that we finally integrate the attachment plugin "Attach Media to Post" into the core of jetengine he is here to solve the upload attachment problem seen for the first time here
so each agent / user will have their uploads attached correctly by default.
And above all it is still well hidden and is quickly forgotten. I'm even sure that 90% of people are not yet aware that their uploads are not attached. its place is really in the jetengine core not in plugin
thank you for your attention