4

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

5

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;
} );
2
  • This will ony work for the grid layout, it will not work on the list layout of the media library. See this answer: wordpress.stackexchange.com/a/271592/22510 Commented Jan 31, 2018 at 17:37
  • Thanks for your comment @ThatBrazilianGuy, yes that's true but it looks like OP was only looking for a way to modify the ajax query request here (not the list layout). I checked the great answer you linked to (by Dave Romsey), and I've already upvoted it in the past ;-)
    – birgire
    Commented Jan 31, 2018 at 17:47
2

Add the filter "ajax_query_attachments_args" in this way and URLs variables are sent to the class WP_Query

the default is to send

array(7) {
  ["orderby"]=>         string(4) "date"
  ["order"]=>           string(4) "DESC"
  ["posts_per_page"]=>  string(2) "40"
  ["paged"]=>           string(1) "1"
  ["post_type"]=>       string(10) "attachment"
  ["post_status"]=>     string(15) "inherit,private"
}

Here you can modify to your liking

add_filter( 'ajax_query_attachments_args', 'my_ajax_query_attachments_args', 1, 10 );

function my_ajax_query_attachments_args($query) {
  $query['date_query'] = array(
    array(
      'before'     => "-1 day",
      'inclusive' => true,
    ),
  );
  var_dump($query);
  return $query;
}  
1
  • I think your priority and arguments are in the wrong order in your add_filter, and you might want to remove that var_dump() from the filter function too.
    – indextwo
    Commented Oct 20, 2016 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.