I have a custom post type "events" that has rewrites / permalink modification in place so that it includes the year and month in the URL (these come from ACF fields). This works perfectly BUT if we have 2 events using the same "slug" then WordPress forces the new one to have a unique postname / slug. I’ve tried Googling this for a bit too long but can’t really find a fix for this.
Example of what is happening (where ‘saint-louis’ is the postname / slug taken from the title):
https://example.com/events/2022/january/saint-louis/
https://example.com/events/2022/february/saint-louis-2/
What I would like to see:
https://example.com/events/2022/january/saint-louis/
https://example.com/events/2022/february/saint-louis/
The closest I got to a fix would be perhaps removing the postname entirely: Custom post type permalink: only use %post_id% and remove %postname%
However: that fix includes the post ID, which I don’t want in the URL. Does anyone have any ideas here? I’ve also considered using another custom field (for ‘city’) so that ‘saint-louis’ wouldn’t be pulling from the postname, but I don’t really know where to start there.
I’ve also attempted to turn hierarchy on / off and the capability to "page" but have had no luck.
My setup currently:
function wdacRegisterEventsPostType() {
/**
* Post Type: Events.
*/
add_rewrite_tag( '%wdac_year%', '([^&]+)' );
add_rewrite_tag( '%wdac_month%', '([^&]+)' );
$labels = [
"name" => __( "Events", "wdac" ),
"singular_name" => __( "Event", "wdac" ),
];
$args = [
"label" => __( "Events", "wdac" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => 'events',
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [
"slug" => "/events/%wdac_year%/%wdac_month%",
"with_front" => false
],
"query_var" => true,
"menu_position" => 5,
"menu_icon" => "dashicons-calendar-alt",
"supports" => [ "title", "editor", "thumbnail" ],
];
register_post_type( "events", $args );
}
add_action( 'init', 'wdacRegisterEventsPostType' );
// Modify events permalink
function wdacModifyEventsURL($permalink, $post) {
$yearMask = '%wdac_year%';
$monthMask = '%wdac_month%';
// Exit early.
if( strpos($permalink, $yearMask) === false && strpos($permalink, $monthMask) === false ) {
return $permalink;
}
// Another exit early
if (!function_exists('get_field')) {
return $permalink;
}
$startDate = strtotime(get_field('wdac_event_start_date', $post->ID));
$year = date("Y", $startDate);
$month = strtolower(date("F", $startDate));
return str_replace([$yearMask,$monthMask], [$year, $month], $permalink );
}
add_filter('post_type_link', 'wdacModifyEventsURL', 1, 3);