Skip to content

Commit 3122337

Browse files
fix: normalize custom post type archive slugs (#2133)
Sanitize configured archive slugs both when ACF saves them and before post type registration. This prevents leading or trailing URL separators from producing rewrite rules that WordPress can never match, while preserving existing normalized slugs and fallback behavior. Co-authored-by: Sebastian Thulin <sebastianthulin@users.noreply.github.com>
1 parent 0c8077a commit 3122337

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

library/AcfFields/php/options-theme-custom-post-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
'name' => 'slug',
5555
'aria-label' => '',
5656
'type' => 'text',
57-
'instructions' => __('Public /url/ to show archive on.', 'municipio'),
57+
'instructions' => __('Public URL slug for the archive, without leading or trailing slashes.', 'municipio'),
5858
'required' => 0,
5959
'conditional_logic' => 0,
6060
'wrapper' => array(

library/Content/CustomPostType.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function __construct()
1515
return $value;
1616
}, 10, 3);
1717

18+
// Keep stored and runtime rewrite slugs consistent with WordPress rewrite paths.
19+
add_filter('acf/update_value/key=field_56b36c01e0619', static function ($value, $post_id, $field) {
20+
return sanitize_title($value);
21+
}, 10, 3);
22+
1823
//Use page or single template
1924
add_filter('single_template', array($this, 'setPageTemplate'), 20);
2025

@@ -73,13 +78,16 @@ public function registerCustomPostTypes()
7378
}
7479

7580
$rewrite = ['with_front' => isset($typeDefinition['with_front']) ? $typeDefinition['with_front'] : true];
81+
$slug = !empty($typeDefinition['slug'])
82+
? sanitize_title($typeDefinition['slug'])
83+
: '';
7684

77-
if (!empty($typeDefinition['slug'])) {
78-
$rewrite['slug'] = $typeDefinition['slug'];
85+
if (!empty($slug)) {
86+
$rewrite['slug'] = $slug;
7987
}
8088

81-
$restBase = !empty($typeDefinition['slug'])
82-
? sanitize_title($typeDefinition['slug'])
89+
$restBase = !empty($slug)
90+
? $slug
8391
: sanitize_title($typeDefinition['post_type_name']);
8492

8593
$args = array(

0 commit comments

Comments
 (0)