php - Foreach skip user by capability -
i have code:
foreach( get_users() $user ) { // set user id $user_id = $user->data->id; // users contributors or above if (!user_can( $user_id, 'edit_posts' ) ) return; // rest of code here }
as can see, have set affects users can edit_posts
. not working, can not use if (!user_can( $user_id, 'edit_posts' ) ) return;
within foreach
or doing wrong?
it looks wish run code if user_can
function returns value.
you have 2 options here; first, closer have, uses continue
control structure:
foreach( get_users() $user ) { // set user id $user_id = $user->data->id; // users contributors or above if (!user_can( $user_id, 'edit_posts' ) ) continue; // rest of code here }
however, lots of developers believe if need use continue
you've got poorly written code somewhere. matter of opinion, opt option 2, place code wish run inside if
block:
foreach( get_users() $user ) { // set user id $user_id = $user->data->id; // users contributors or above if ( user_can( $user_id, 'edit_posts' ) ){ // rest of code here } }
Comments
Post a Comment