diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js
index b4afb9a979949..2b4bb0709356b 100644
--- a/packages/block-library/src/post-author-name/edit.js
+++ b/packages/block-library/src/post-author-name/edit.js
@@ -13,7 +13,7 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
-import { __ } from '@wordpress/i18n';
+import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { PanelBody, ToggleControl } from '@wordpress/components';
@@ -22,9 +22,10 @@ function PostAuthorNameEdit( {
attributes: { textAlign, isLink, linkTarget },
setAttributes,
} ) {
- const { authorName } = useSelect(
+ const { authorName, supportsAuthor } = useSelect(
( select ) => {
- const { getEditedEntityRecord, getUser } = select( coreStore );
+ const { getEditedEntityRecord, getUser, getPostType } =
+ select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
postType,
@@ -33,6 +34,8 @@ function PostAuthorNameEdit( {
return {
authorName: _authorId ? getUser( _authorId ) : null,
+ supportsAuthor:
+ getPostType( postType )?.supports?.author ?? false,
};
},
[ postType, postId ]
@@ -90,7 +93,17 @@ function PostAuthorNameEdit( {
) }
-
{ displayAuthor }
+
+ { supportsAuthor
+ ? displayAuthor
+ : sprintf(
+ // translators: %s: Name of the post type e.g: "post".
+ __(
+ 'This post type (%s) does not support the author.'
+ ),
+ postType
+ ) }
+
>
);
}
diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php
index effc83962a354..243d78ca70129 100644
--- a/packages/block-library/src/post-author-name/index.php
+++ b/packages/block-library/src/post-author-name/index.php
@@ -26,6 +26,10 @@ function render_block_core_post_author_name( $attributes, $content, $block ) {
return '';
}
+ if ( ! post_type_supports( $block->context['postType'], 'author' ) ) {
+ return '';
+ }
+
$author_name = get_the_author_meta( 'display_name', $author_id );
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$author_name = sprintf( '%3$s', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $author_name );
diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js
index 6186b0d052e8a..dd2b3aa617548 100644
--- a/packages/block-library/src/post-author/edit.js
+++ b/packages/block-library/src/post-author/edit.js
@@ -21,7 +21,7 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
-import { __ } from '@wordpress/i18n';
+import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
const minimumUsersForCombobox = 25;
@@ -38,9 +38,9 @@ function PostAuthorEdit( {
setAttributes,
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
- const { authorId, authorDetails, authors } = useSelect(
+ const { authorId, authorDetails, authors, supportsAuthor } = useSelect(
( select ) => {
- const { getEditedEntityRecord, getUser, getUsers } =
+ const { getEditedEntityRecord, getUser, getUsers, getPostType } =
select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
@@ -52,6 +52,8 @@ function PostAuthorEdit( {
authorId: _authorId,
authorDetails: _authorId ? getUser( _authorId ) : null,
authors: getUsers( AUTHORS_QUERY ),
+ supportsAuthor:
+ getPostType( postType )?.supports?.author ?? false,
};
},
[ postType, postId ]
@@ -97,6 +99,18 @@ function PostAuthorEdit( {
const showAuthorControl =
!! postId && ! isDescendentOfQueryLoop && authorOptions.length > 0;
+ if ( ! supportsAuthor ) {
+ return (
+
+ { sprintf(
+ // translators: %s: Name of the post type e.g: "post".
+ __( 'This post type (%s) does not support the author.' ),
+ postType
+ ) }
+
+ );
+ }
+
return (
<>
diff --git a/packages/block-library/src/post-author/index.php b/packages/block-library/src/post-author/index.php
index faf894d997d73..2d01de508b94a 100644
--- a/packages/block-library/src/post-author/index.php
+++ b/packages/block-library/src/post-author/index.php
@@ -26,6 +26,10 @@ function render_block_core_post_author( $attributes, $content, $block ) {
return '';
}
+ if ( ! post_type_supports( $block->context['postType'], 'author' ) ) {
+ return '';
+ }
+
$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar(
$author_id,
$attributes['avatarSize']