Here’s how can you get the ID and email address of the author of the currently being edited post:
// Get the post author's ID.
let postAuthorId = wp.data.select( 'core/editor' ).getCurrentPostAttribute( 'author' );
/* or you can also use this:
let postAuthorId = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'author' );
*/
// Get the post author's email.
let postAuthorEmail = wp.data.select( 'core' ).getUser( postAuthorId )?.email;
console.log( postAuthorId, postAuthorEmail );
Note: I used the optional chaining operator (i.e. ?.
) as in ?.email
because getUser()
uses AJAX, so you may need to call it twice in order to see the proper user object/data.
As for the issue in question – “However; it doesn’t get me the email and it doesn’t get me the user id of the currently-being-edited post“, that’s because the context
is view
, therefore the REST API did not include the email
field which is made available only to the context edit
.
-
See the row for the
email
field in the schema table at https://developer.wordpress.org/rest-api/reference/users/#schema. -
wp.data.select( 'core' ).getUser()
sets the default context toedit
, so if you omitted thecontext
prop or that you usedcontext:'edit'
, then you would have seen theemail
field in the object returned by thatgetUser
function.