Skip to content

Conversation

BugReportOnWeb
Copy link
Contributor

What?

Closes #55949

This PR adds a "Post Type Label" variation to the core/query-title block, which displays the singular name of the queried post type.

Why?

Currently, there’s no way to dynamically display the post type label in a query context. This variation extends core/query-title to support built-in and custom post types.

How?

  • Added new variation "Post Type Label" to display the queried post type's singular name.
  • Retrieves the post type from block context or default to the current post type.

Testing Instructions

  1. Add multiple Query Loop blocks.
  2. Change query settings and configure for different post types (posts, pages, custom post types).
  3. Add a 'Post Type Label' block inside the Query Loop block.
  4. Test the setting for displaying the Post Type prefix.
  5. Save the post and check the frontend.
  6. Confirm that post type labels are rendered correctly in both the editor and frontend.

Screenshots or screencast

Using "Post Type Label" under each post title to show the queried post type (with and without prefix).

Screenshot 2025-08-11 at 9 28 18 PM

@BugReportOnWeb BugReportOnWeb marked this pull request as ready for review August 11, 2025 16:40
Copy link

github-actions bot commented Aug 11, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: BugReportOnWeb <[email protected]>
Co-authored-by: ryanwelcher <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: bhubbard <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ryanwelcher ryanwelcher added [Feature] Blocks Overall functionality of blocks [Type] Feature New feature to highlight in changelogs. labels Aug 11, 2025
export function usePostTypeLabel( contextPostType ) {
const currentPostType = useSelect( ( select ) => {
// eslint-disable-next-line @wordpress/data-no-store-string-literals
const { getCurrentPostType } = select( 'core/editor' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import the store name here instead of suppressing the eslint error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review!

I did this to avoid having @wordpress/editor as a dependency for @wordpress/block-editor. A similar pattern is used in useArchiveLabel:

// @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor, so to avoid
// declaring @wordpress/editor as a dependency, we must access its
// store by string.
// The solution here is to split WP specific blocks from generic blocks.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
const { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } =
select( 'core/editor' );

Would it help if I add a comment referencing this?

Copy link
Contributor

@ryanwelcher ryanwelcher Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the reference, yes. That way there is no confusion for reviewers or other engineers who reference this code. Thanks for the explanation!

@t-hamano t-hamano added the [Block] Query Title Affects the Query Title Block label Aug 13, 2025
Copy link
Contributor

@ryanwelcher ryanwelcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the comment has been added I mentioned above, I'm happy with the code.

However, I'd like to get a second opinion from @t-hamano or @Mamaduka just to be sure as I am a little out of practice with the code here and don't want to miss anything.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I left some minor feedback, but I think the overall approach is correct.

I also understand that for this block to work outside of a query context, we have to hard-code the editor store name.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Note that failing tests aren't related to this PR. They're a temporary problem related to the .org server. See: https://wordpress.slack.com/archives/C02QB2JS7/p1755184038359589

@t-hamano t-hamano merged commit c880903 into WordPress:trunk Aug 14, 2025
89 of 117 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.5 milestone Aug 14, 2025
@bph bph added [Type] Enhancement A suggestion for improvement. and removed [Type] Feature New feature to highlight in changelogs. labels Aug 21, 2025
@Drivingralle
Copy link

Drivingralle commented Sep 10, 2025

Can we add support for paragaph-tag for the block?

@t-hamano
Copy link
Contributor

@Drivingralle I think it's possible. Please feel free to submit an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Title Affects the Query Title Block [Feature] Blocks Overall functionality of blocks [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block to display Custom Post Type Name
5 participants