[wp-trac] [WordPress Trac] #20861: switch_to_blog() breaks custom post type permalinks
WordPress Trac
noreply at wordpress.org
Thu Sep 17 16:50:37 UTC 2020
#20861: switch_to_blog() breaks custom post type permalinks
--------------------------+----------------------
Reporter: sickhippie | Owner: (none)
Type: defect (bug) | Status: closed
Priority: normal | Milestone:
Component: Multisite | Version:
Severity: normal | Resolution: wontfix
Keywords: close | Focuses:
--------------------------+----------------------
Comment (by mennoll):
I was able to make a fix you can put in your themes `functions.php` file.
It works when your permalink structure is set to `/%postname%/` .
I have not tested other permalink structures, but maybe this can still
help you to find the right solution.
{{{#!php
<?php
/**
* Fix multisite custom post type permalinks after switching blogs
* @see https://core.trac.wordpress.org/ticket/20861
*
* @param string $post_link
* @param WP_Post $post
* @return string
*/
function fix_multisite_cpt_permalinks( $post_link, $post ) {
// Only run if switched to other site
if ( ! isset( $GLOBALS['switched'] ) || !
$GLOBALS['switched'] || empty( $GLOBALS['_wp_switched_stack'] ) ) {
return $post_link;
}
// Only run if permalink structure is %postname%
$current_permalink_structure = get_option(
'permalink_structure' );
if ( $current_permalink_structure !== '/%postname%/' ) {
return $post_link;
}
// Get post type url prefix
$url_prefix = $post->post_type;
$post_type_object = get_post_type_object( $post->post_type
);
if ( $post_type_object instanceof WP_Post_Type && ! empty(
$post_type_object->rewrite ) && array_key_exists( 'slug',
$post_type_object->rewrite ) ) {
$url_prefix = $post_type_object->rewrite['slug'];
}
// Generate post permalink
$post_link = home_url( '/' . $url_prefix . '/' .
$post->post_name . '/' );
return $post_link;
}
add_filter( 'post_type_link', 'fix_multisite_cpt_permalinks', 10,
2 );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/20861#comment:19>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list