WordPress.org

Ready to get started?Download WordPress

Forums

Permalink displays incorrect URL when using switch_to_blog() (10 posts)

  1. jonathandietz
    Member
    Posted 3 years ago #

    Permalink displays incorrect URL when using switch_to_blog()

    I'm running WordPress MU 2.9.2 with multiple blogs setup as subdomains. I'm trying to display excerpts of posts from one blog (blog-A) onto another (blog-B). When I run through the loop, the links generated from functions like the_permalink(), the_category(), or comments_popup_link() display the URL path as though the post is generated from blog-B so when a user clicks on a link they are taken to a 404 page.

    Here is the code I'm using:

    <?php switch_to_blog($blog_id); ?>
    <?php query_posts("posts_per_page={$posts_per_page}&cat={$cat_id}"); ?>
    <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
    
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="date"><?php the_time('l, F jS, Y') ?></div>
    
        <?php the_post_thumbnail( 'blog-post' ); ?>
    
        <div class="entry">
        	<?php the_excerpt(); ?>
        </div>
    
        <div class="info">
          <span class="category">Posted in <?php the_category(', ') ?></span> |
          <?php edit_post_link('Edit', '', ' | '); ?>
          <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
        </div>
      </div>
    
      <?php endwhile; ?>
    <?php endif; ?>
    <?php restore_current_blog(); ?>

    The output of URLs generated is http://blog-b.example.com/xxxx/xx/xx/blog-title when they should be http://blog-a.example.com/xxxx/xx/xx/blog-title

    Any idea what the problem might be?

  2. switch_to_blog($blog_id);

    I think you actually have to put a number in there, the ID of the blog you're switching to.

  3. jonasbjork
    Member
    Posted 3 years ago #

    Sometimes this is needed before switch_to_blog() :
    global $switched;

    If you use W3 Total Cache I have noticed a bug in Option Cache that makes switch_to_blog() behave strange.

  4. jonathandietz
    Member
    Posted 3 years ago #

    $blog_id is a variable that I'm setting elsewhere.

    It is an integer and ends up being something like

    switch_to_blog(1);

  5. jonasbjork
    Member
    Posted 3 years ago #

    Sorry, that would be Object Cache, not Option Cache.. :)

  6. jonathandietz
    Member
    Posted 3 years ago #

    I added global $switched; before switch_to_blog() but its still not working. W3 Total Cache isn't activated for the one that is displaying the excerpts (blog-b) but it might be for the source blog (blog-a).

    Just so I'm clear, switch_to_blog() does work and switches to another blog and pulls in content from the other blog. The post content is correct except for the links. All of the links in the loop point back to the blog used to display the excerpts (blog-b) even though the content is coming from another blog (blog-a)

  7. jonathandietz
    Member
    Posted 3 years ago #

    Thanks @jonasbjork. That put my on the right track. W3 Total Cache was recently enabled so I started looking into the W3 Total Cache / switch_to_blog correlation.

    I found a function to clear the cache and a few posts of people saying this worked for them.

    What I was add this wp_cache_flush(); onto my page and then the page started displaying the correct URLs. However, as soon as I removed that function the incorrect URLs return.

    At least I'm on the right track now :)

  8. jonathandietz
    Member
    Posted 3 years ago #

    Found the bug with Object Caching here: http://core.trac.wordpress.org/ticket/14992

  9. Simon Wheatley
    Code for the People
    Posted 3 years ago #

    I think I may have a W3 Total Cache specific solution to your problem: see this post.

  10. jonathandietz
    Member
    Posted 3 years ago #

    Awesome. I read your post and will give it a try. Thanks Simon!

Topic Closed

This topic has been closed to new replies.

About this Topic