Title: get_permalink after switch_to_blog function
Last modified: February 12, 2019

---

# get_permalink after switch_to_blog function

 *  [mishmasha](https://wordpress.org/support/users/mishmasha/)
 * (@mishmasha)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/)
 * Hey guys!
 * We have multisite, that is handles different languages of same site (mainsite.
   com, mainsite.com/it – for italian version, mainsite.com/de for germany, etc)
   
   We have custom post types on the site (for example “books”) with TRANSLATED url
   rewrite slug (like mainsite.com/books/book1, mainsite.com/it/libri/prenota1, 
   mainsite.com/de/bucher/buch1) – we add translated slug when register_post_type
   is launched . Also we store connection between these posts in the database (so
   they linked). For such posts we want to show, that they have translated versions.
 * We was trying to use native WP multisite functional like switch_to_blog() and
   get_permalink() functions, but unfortunately we’ve stacked, as get_permalink()
   returns link using CURRENT rewrite slug
    It shows mainsite.com/it/books/prenota1,
   mainsite.com/de/books/buch1 when you’re on the main site and mainsite.com/libri/
   book, mainsite.com/de/libri/buch1 when you’re on Italian version.
 * Is it correct that after switch_to_blog() function permalink structure remains
   the same? because in such situation using native WP functional is not really 
   useful 🙁

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11198663)
 * You don’t say the order of execution of your code, but switch_to_blog will switch
   out the object cache, so the variables you are working with for the query should
   be switched. I didn’t follow the code all the way down to see if the query is
   done again on the other blog, but it doesn’t seem like it would be since it _is_
   a separate blog.
    The point is that get_permalink uses the post passed or the
   global post, so you have to check the code to see if you need to repeat the query
   on the switched blog to get the post on that one. Or just use a filter to compute
   the permalink for the other sites, based on the current one.
 *  Thread Starter [mishmasha](https://wordpress.org/support/users/mishmasha/)
 * (@mishmasha)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11200395)
 * Hey Joy! Thanks for the response.
    Main thing, I’m getting correct page, except
   this wrong untraslated rewrite slug. I can show this part (will remove additional
   checks for better reading)
 *     ```
       foreach( $language_blogs as $blog_id => $language_id ) {
                   if (!array_key_exists($blog_id, $linked_pages))
                       continue;
   
                   switch_to_blog($blog_id);
   
                   $post_id = $linked_pages[$blog_id];
                   $url = get_permalink($post_id);
                   $links[$language_id] = $url;
   
                   restore_current_blog();
       }
       ```
   
 * As you can see, I pass correct post ID to the get_permalink function. But it 
   is being built using permalink structure from CURRENT blog, and not the switched
   one.
    So don’t understand if I’m doing something wrong?
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11202083)
 * That is exactly what I said last time. The switch_to_blog swaps the global variables(
   like the object cache which is what is retrieved from the query), but if you 
   didn’t retrieve anything from the other blog, you can’t expect it to work. You
   would have to do a new query on the other blog. They are **separate**.
 *  Thread Starter [mishmasha](https://wordpress.org/support/users/mishmasha/)
 * (@mishmasha)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11209214)
 * Thanks, I understand that they are separate. I don’t understand how I can get
   URL of the linked page by post ID from **another** blog using **another** blog
   permalink structure.
    What should I query first?
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11210551)
 * If you have the correct post ID for the **other blog**, I think you could do 
   a get_post(). If you don’t have the correct ID, you need to do whatever query
   you need to get it.
    Be sure to switch back when you are done.
 *  [Paul Ryan](https://wordpress.org/support/users/figureone/)
 * (@figureone)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11790836)
 * Aloha, just chiming in here since I discovered this bug as well. It is an actual
   bug (sorry [@joyously](https://wordpress.org/support/users/joyously/)), but was
   reported in Trac in 2012 and closed as `wontfix` (due to being difficult and 
   not performant to fix):
    [https://core.trac.wordpress.org/ticket/20861](https://core.trac.wordpress.org/ticket/20861)

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘get_permalink after switch_to_blog function’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [multisite](https://wordpress.org/support/topic-tag/multisite/)
 * [Permalink structure](https://wordpress.org/support/topic-tag/permalink-structure/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [Paul Ryan](https://wordpress.org/support/users/figureone/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/get_permalink-after-switch_to_blog-function/#post-11790836)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
