我不知道你为什么pre_get_posts
因为这是正确使用的挂钩。例如,如果我们想限制edit.php
当当前用户是作者时,屏幕仅显示作者的帖子,我们可以说:
/**
* Pre Get Posts
* Restrictions by Role
*
* @return void
*/
function wpse378774_pgp( $query ) {
// Return Early - Not Admin Panel
if( ! is_admin() ) {
return;
// Return Early - Not Main Query
} else if( ! $query->is_main_query() ) {
return;
}
$user = wp_get_current_user();
// Return Early - User not an author
if( ! in_array( \'author\', (array)$user->roles ) ) {
return;
}
// Limit all queries to only show posts by author
if( $query->is_main_query() ) {
// Get all user IDs who are authors
$user_query = new WP_User_Query( array(
\'role\' => \'author\',
\'fields\' => \'ID\',
) );
$user_ids = $user_query->get_results();
// Set our query modifier
$query->set( \'author__in\', $user_ids );
}
}
add_action( \'pre_get_posts\', \'wpse378774_pgp\' );
有关更多信息,请查看
WP_Query 和
WP_User_Query.