admin管理员组

文章数量:1297200

Inside the loop, when logged in to a localhost test site as $user->ID == 1 the function is_author(get_current_user_id()) returns false when get_the_author_meta('ID') returns 1. Thus, the following conditional is never executed (is_user_logged_in() returns true):

if( is_user_logged_in() && is_author(get_current_user_id()) ) {
    // do stuff
}

Have I missed something obvious?

Inside the loop, when logged in to a localhost test site as $user->ID == 1 the function is_author(get_current_user_id()) returns false when get_the_author_meta('ID') returns 1. Thus, the following conditional is never executed (is_user_logged_in() returns true):

if( is_user_logged_in() && is_author(get_current_user_id()) ) {
    // do stuff
}

Have I missed something obvious?

Share Improve this question asked Aug 18, 2020 at 19:15 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 1
  • 3 What are you trying to do with that is_author()? It would return true only on author archives, e.g. example.com/author/admin – Sally CJ Commented Aug 18, 2020 at 19:43
Add a comment  | 

2 Answers 2

Reset to default 4

Are you sure you're doing it within the loop? The is_author function checks if $wp_query is set.

Another option would be to try this:

$current_user = wp_get_current_user();

if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
    // do stuff
}

another issue might be if you are inside a WP REST API request and did not pass the X-WP-nonce header

本文标签: functionsisauthor(getcurrentuserid()) returns false when author id and user id match