Get the email of the author of the currently being edited post in Gutenberg frontend

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 to edit, so if you omitted the context prop or that you used context:'edit', then you would have seen the email field in the object returned by that getUser function.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)