author_can()
Checks whether the author of the supplied post has a specific capability.
1 time — 0.002667 sec (very slow) | 50000 times — 1.86 sec (fast) | PHP 7.1.11, WP 4.9.4
No Hooks.
Return
true|false
. Whether the post author has the given capability.
Usage
author_can( $post, $capability );
- $post(int/WP_Post) (required)
- Post ID or post object.
- $capability(string) (required)
- Capability name.
Examples
#1 Check whether the author of the current post ($post->ID) can publish posts:
if( author_can( $post->ID, 'publish_posts' ) ){ echo 'The author of the post can publish posts'; }
Changelog
Since 2.9.0 | Introduced. |
Since 5.3.0 | Formalized the existing and already documented ...$args parameter by adding it to the function signature. |
author_can() author can code WP 6.7.1
function author_can( $post, $capability, ...$args ) { $post = get_post( $post ); if ( ! $post ) { return false; } $author = get_userdata( $post->post_author ); if ( ! $author ) { return false; } return $author->has_cap( $capability, ...$args ); }