I am trying to limit the files that are shown in the wordpress media library popup (from wp_editor).

Currently every single file that I have ever uploaded to my site is shown in the library but I would like to limit what users see to just files uploaded in the last 24 hours.

It is possible to limit the media library by author using the following code; however I’m not even sure where to start to limit the media library popup to files uploaded in the last 24 hours.

add_action('pre_get_posts','users_own_attachments');
function users_own_attachments( $wp_query_obj ) {

global $current_user, $pagenow;

if( !is_a( $current_user, 'WP_User') )
    return;

if( !in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) )
    return;

if( !current_user_can('delete_pages') )
    $wp_query_obj->set('author', $current_user->ID );

return;
}

2 Answers
2

You can adjust the attachment query in the media library popup, through the ajax_query_attachments_args filter.

Here are two PHP 5.4+ examples:

Example #1:

Show only attachments that where uploaded during the last 24 hours:

/**
 * Media Library popup 
 * - Only display attachments uploaded during the last 24 hours:
 */
add_filter( 'ajax_query_attachments_args', function( $args )
{
   $args['date_query'] = [['after' => '24 hours ago', 'inclusive' => true ]];
   return $args;
} );

Example #2:

Show only attachments that where uploaded during the last 24 hours by the current user:

/**
 * Media Library popup
 *    - Only display attachments uploaded during the last 24 hours by the current user:
 */
add_filter( 'ajax_query_attachments_args', function( $args )
{
   $args['author']     = get_current_user_id();
   $args['date_query'] = [['after' => '24 hours ago', 'inclusive' => true ]];
   return $args;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *