如果您只想在通过AJAX加载附件时过滤附件,例如,在设置帖子特色图像时,可以使用:
function filter_get_the_user_attachments( $query ) {
$current_user = wp_get_current_user();
if ( !$current_user ) {
return;
}
$current_user_id = $current_user->ID;
$query[\'author__in\'] = array(
$current_user_id
);
return $query;
};
add_filter( \'ajax_query_attachments_args\', \'filter_get_the_user_attachments\', 10 );
与上一个答案中的片段类似,但速度更快。
此外,如果您需要筛选上载时加载的附件。您可以使用的php页面:
function action_get_the_user_attachments( $query ) {
// If we are not seeing the backend we quit.
if ( !is_admin() ) {
return;
}
/**
* If it\'s not a main query type we quit.
*
* @link https://codex.wordpress.org/Function_Reference/is_main_query
*/
if ( !$query->is_main_query() ) {
return;
}
$current_screen = get_current_screen();
$current_screen_id = $current_screen->id;
// If it\'s not the upload page we quit.
if ( $current_screen_id != \'upload\' ) {
return;
}
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$author__in = array(
$current_user_id
);
$query->set( \'author__in\', $author__in );
}
add_action( \'pre_get_posts\', \'action_get_the_user_attachments\', 10 );
希望这有帮助。