从wp媒体库中隐藏旧附件

时间:2015-08-16 作者:ameeromar

我正在尝试限制wordpress媒体库弹出窗口(来自wp\\U编辑器)中显示的文件。

目前,我上传到网站的每个文件都会显示在库中,但我想限制用户只看到过去24小时上传的文件

可以使用以下代码按作者限制媒体库;然而,我甚至不知道从哪里开始将媒体库弹出窗口限制为过去24小时上载的文件。

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 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以通过ajax_query_attachments_args 滤器

以下是两个PHP 5.4+示例:

Example #1:

仅显示过去24小时内上载的附件:

/**
 * 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:

仅显示当前用户在过去24小时内上载的附件:

/**
 * 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;
} );

SO网友:Salem Terrano

以这种方式添加过滤器“ajax\\u query\\u attachments\\u args”,并将URL变量发送到类WP\\u query

默认设置为发送

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"
}
在这里,您可以根据自己的喜好进行修改

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;
}  

结束