This isn’t the cleanest of code, but it seems to have solved my issue, hopefully not introduced new ones.
Main problem was that get_post_by_custom_permalink function seemed to always remove trailing slash when trying to lookup custom permalinks. So it is no real wonder why trailing slash permalinks are never found.
/**
* Fetch a single post based on the custom permalink value stored as custom
* meta data.
*
* @param string $permalink
*/
function get_post_by_custom_permalink( $permalink, $exclude = false, $suffix = '' )
{
$post = false;
if ( $front = $this->front() ) {
$permalink = str_replace( $front, '', $permalink );
}
// Fetch all the public post types to lookup against...
if ( $post_types = get_post_types( array( 'public' => true ), 'names' ) ) {
if ( $posts = get_posts( array(
'post_type' => $post_types,
'meta_key' => '_' . $this->tag . $suffix,
'meta_value' => $permalink,
'posts_per_page' => 1,
'exclude' => $exclude
) ) ) {
$post = array_shift( $posts );
} else if ( substr( $permalink, -1 ) != '/' ) {
// Lookup with a slash appended, just in case...
$post = $this->get_post_by_custom_permalink(
trailingslashit( $permalink ),
$exclude,
$suffix
);
} else if ( substr( $permalink, 0, 1 ) == '/' ) {
// Lookup without a slash prefix, this is the ideal behaviour
// however the above is required for backwards compatability.
$post = $this->get_post_by_custom_permalink(
ltrim( $permalink, '/' ),
$exclude,
$suffix
);
} else if ( substr( $permalink, -1 ) != '/' ) {
// Lookup with a slash appended, just in case...
$post = $this->get_post_by_custom_permalink(
trailingslashit( $permalink ),
$exclude,
$suffix
);
}
}
return $post;
}