[wp-trac] [WordPress Trac] #20861: switch_to_blog() breaks custom post type permalinks

WordPress Trac noreply at wordpress.org
Thu May 17 20:14:26 UTC 2018


#20861: switch_to_blog() breaks custom post type permalinks
--------------------------+-----------------------
 Reporter:  sickhippie    |       Owner:  (none)
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:
Component:  Multisite     |     Version:
 Severity:  normal        |  Resolution:
 Keywords:  close         |     Focuses:
--------------------------+-----------------------
Changes (by danielpataki):

 * status:  closed => reopened
 * resolution:  wontfix =>


Comment:

 We "fixed" this issue by looking at rewrite rules. This works for our
 purposes but I haven't done extensive research to see if this would break
 anywhere. Perhaps it can spark some interest though.

 {{{#!php
 <?php
     switch_to_blog(3);
     $url = get_permalink(1234);
     $post_type = get_post_type(1234);
     $post_type_object = get_post_type_object($post_type);

     if(!empty($post_type_object->rewrite) && array_key_exists('slug',
 $post_type_object->rewrite) ) {
       $rewrite = get_option('rewrite_rules');
       $rule = array_search('index.php?post_type=' . $post_type, $rewrite);
       $base = str_replace('/?$', '', $rule);
       $url = str_replace('/' . $post_type_object->rewrite['slug'] . '/',
 '/' . $base . '/', $url);
     }

     $translations[$i]['url'] = $url;
     restore_current_blog();
 }}}

 This is not quite the exact code we use but it's a useful example. I'm
 super unsure about the quick and dirty str_replace bits at the end, it
 feels like spit and duct tape. I hope this is useful for someone, if
 anyone has a better idea, it would be great to get this one moving :)

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


More information about the wp-trac mailing list