[wp-trac] [WordPress Trac] #59766: Custom post type archive URLs are being resolved in an unexpected way if a page with the same slug exists and the URL's casing doesn't exactly match the custom post type's rewrite slug

WordPress Trac noreply at wordpress.org
Mon Oct 30 14:50:54 UTC 2023


#59766: Custom post type archive URLs are being resolved in an unexpected way if a
page with the same slug exists and the URL's casing doesn't exactly match
the custom post type's rewrite slug
-------------------------------+------------------------------
 Reporter:  nonverbla          |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:  6.3.2
 Severity:  normal             |  Resolution:
 Keywords:  has-patch          |     Focuses:
-------------------------------+------------------------------

Comment (by nonverbla):

 == Temporary workaround

 As a temporary workaround I implemented this filter for
 `rewrite_rules_array` in my theme (just for reference):

 {{{#!php
 <?php
 /**
  * Make all archive rewrite rules case-insensitive
  */
 function rah_make_archives_rewrite_rules_case_insensitive(array $rules):
 array
 {
     $new_rules = [];

     $archive_slugs = array_map(
         fn ($post_type) =>
 (get_post_type_object($post_type))->has_archive,
         get_post_types([
             'has_archive' => true,
             '_builtin' => false
         ])
     );

     /** Produces something like "cpt1|cpt2|cpt3" */
     $joined_archive_slugs = implode("|", $archive_slugs);

     /**
      * Walk through all rules, and if one matches one of the
      * post type slugs at the beginning, make it case-insensitive
      */
     foreach ($rules as $rule => $rewrite) {
         if (preg_match("/^($joined_archive_slugs)/", $rule)) {
             $new_rules["(?i)$rule"] = $rewrite;
             continue;
         }
         $new_rules[$rule] = $rewrite;
     }

     return $new_rules;
 }
 add_filter('rewrite_rules_array',
 'rah_make_archives_rewrite_rules_case_insensitive');
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59766#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list