WordPress 多媒体库只显示当前用户上传的文件

在 wordpress 的 media library 里只显示当前用户上传的文件。也试用于 acf_form

//wordpress show only media user has uploaded 
add_action('pre_get_posts','ml_restrict_media_library');

function ml_restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    if( !is_a( $current_user, 'WP_User') )
    	return;
    if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
    	return;
    if( !current_user_can('manage_media_library') )
    	$wp_query_obj->set('author', $current_user->ID );
    	return;
}

is_a() 函数已废弃,自 PHP 5 起使用 instanceof 类型运算符。上例在 PHP 5 中会是这样:

//wordpress show only media user has uploaded 
add_action('pre_get_posts','ml_restrict_media_library');

function ml_restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    //if( !is_a( $current_user, 'WP_User') )
    if ($current_user instanceof WP_User)
    	return;
    if ( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
    	return;
    if ( !current_user_can('manage_media_library') )
    	$wp_query_obj->set('author', $current_user->ID );
    	return;
}

Comments

421 responses to “WordPress 多媒体库只显示当前用户上传的文件”