WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 5 months ago

#49168 new defect (bug)

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

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

Description

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 5 months ago.
Patch from https://github.com/WordPress/WordPress/pull/452

Download all attachments as: .zip

Change History (3)

#1 @maurisrx
5 months ago

  • Keywords has-patch added

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

#2 @maurisrx
5 months 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.