Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#49168 new defect (bug)

is_post_type_archive( $post_types = '' ) doesn't work with underscore (_)

Reported by: maurisrx's profile maurisrx Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.2
Component: Query Keywords: has-patch
Focuses: template Cc:


I define my custom post type slug using underscore, e.g. 'post_type'. But it always returns false when I try to check with is_post_type_archive( 'post_type' ). It turns out I have to use hyphen (-) instead of underscore (_), e.g. is_post_type_archive( 'post-type' ).

It is confusing because I defined the CPT using underscore, but the is_post_type_archive() doesn't accept underscore.

The issue is is_post_type_archive() uses $post_type_object->name instead of $post_type_object->slug to check against. For backward compatibility, should we accept both underscore (_) and hyphen (-) in the is_post_type_archive( $post_types )?

Attachments (1)

49168.diff (544 bytes) - added by maurisrx 4 years ago.
Patch from

Download all attachments as: .zip

Change History (3)

#1 @maurisrx
4 years ago

  • Keywords has-patch added

I attached a patch that accepts both hyphen (-) and underscore (_) as function argument.

#2 @maurisrx
4 years ago

This turns out happens because I use Custom Post Type UI plugin to define my CPT as it converts underscore to hyphen in $post_type_object->name. If I define the CPT using register_post_type() directly in my plugin or theme, this issue doesn't appear.

Still, is it better if we use $post_type_object->slug to check against in the is_post_type_archive() instead of $post_type_object->name?

Note: See TracTickets for help on using tickets.