Skip to content

Conversation

yogeshbhutkar
Copy link
Contributor

@yogeshbhutkar yogeshbhutkar commented Jun 30, 2025

What, Why, and How?

Closes #70558

This PR adds the logic to order Post Types using locale-aware string comparison, making it easier for users to find specific post types in the list.

Testing Instructions

  1. Make sure to register some custom post types.
Example
function register_book_type() {
	$labels = array(
		'name'                  => _x( 'Book', 'Post type general name', 'book' ),
		'singular_name'         => _x( 'Book', 'Post type singular name', 'book' ),
		'menu_name'             => _x( 'Books', 'Admin Menu text', 'book' ),
		'name_admin_bar'        => _x( 'Book', 'Add New on Toolbar', 'book' ),
		'add_new'               => __( 'Add New', 'book' ),
		'add_new_item'          => __( 'Add New Book', 'book' ),
		'new_item'              => __( 'New Book', 'book' ),
		'edit_item'             => __( 'Edit Book', 'book' ),
		'view_item'             => __( 'View Book', 'book' ),
		'all_items'             => __( 'All Books', 'book' ),
		'search_items'          => __( 'Search Books', 'book' ),
		'parent_item_colon'     => __( 'Parent Books:', 'book' ),
		'not_found'             => __( 'No Books found.', 'book' ),
	);
	$args   = array(
		'labels'             => $labels,
		'description'        => 'Book custom post type.',
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => 20,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail' ),
		'taxonomies'         => array( 'category', 'post_tag' ),
		'show_in_rest'       => true,
	);

	register_post_type( 'Book', $args );
}

add_action( 'init', 'register_book_type' );
  1. Navigate to Appearance -> Editor -> Templates and click on the Add Template button.
  2. Confirm that the Archive: and Single item: values are all alphabetically ordered.

Testing Instructions for Keyboard

Same.

Screenshots

Before After
before after

@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review June 30, 2025 07:15
Copy link

github-actions bot commented Jun 30, 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: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>

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

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. [Feature] Templates API Related to API powering block template functionality in the Site Editor labels Jun 30, 2025
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.

I think we can move forward with this PR, but we might want to get some additional feedback just to be sure.

The important points are:

  • This PR does not sort all templates alphabetically. It only sorts templates related to post types. Non-post type templates have a fixed sort order.
  • One thing to note is that the sorting of these templates may change depending on the locale if the labels are translatable.

@t-hamano t-hamano requested review from carolinan and a team July 4, 2025 23:32
@jasmussen
Copy link
Contributor

Thanks for the PR, and thanks for the ping. This gets to the heart of it:

This PR does not sort all templates alphabetically. It only sorts templates related to post types.

This is an intentional curation both for templates, and for the block library: it is more likely that you'll want to edit your Front-Page template than your Author Archives template. Similarly, it's more likely you'll want to insert an Image than a Details block. This becomes important for translations as well.

Since this PR doesn't touch that aspect, it seems fine.

@carolinan
Copy link
Contributor

Similarly I was wondering if single post should always be first, even if there are custom post types?
But I understand that was not mentioned in the issue, I don't know if it would be difficult to change.

@t-hamano
Copy link
Contributor

t-hamano commented Jul 7, 2025

I was wondering if single post should always be first, even if there are custom post types?

This isn't hard, but we'll probably have to hard-code the post type string in the logic. Example code:

const usePublicPostTypes = () => {
	const postTypes = useSelect(
		( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),
		[]
	);
	return useMemo( () => {
		return postTypes
			?.filter(
				( { viewable, slug } ) => viewable && slug !== 'attachment'
			)
			.sort( ( a, b ) => {
				if ( a.slug === 'post' || b.slug === 'post' ) {
					return 0;
				}
				return a.name.localeCompare( b.name );
			} );
	}, [ postTypes ] );
};

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!

Before After
before after

Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
@t-hamano t-hamano merged commit ee5a18b into WordPress:trunk Jul 8, 2025
59 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.2 milestone Jul 8, 2025
@carolinan
Copy link
Contributor

Thank you

cbravobernal pushed a commit that referenced this pull request Jul 15, 2025
… modal (#70562)

* fix: sort post types alphabetically

* fix: single post should always be first

* chore: include comments

---------

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template creation in Site Editor: post types are not sorted alphabetically
4 participants