Get Permalink of Network Blog Post

I am trying to write a function that will allow me to specify a post ID and a blog ID, and then have it return the permalink and title of the blog post.

I need this to be efficient because the function will be used in a foreach loop that could cause the function to run upwards of 50 times.

Read More

Any ideas?

Related posts

Leave a Reply

2 comments

  1. As per the suggestion above, I’ve gone for this:

    $html = '<ul>';
    foreach ( $recent_across_network as $post ) {
    
        switch_to_blog( $post->blog_id );
    
        $postURI = get_permalink( $post->ID );          
        $html .= '<li><a href="' . $postURI . '">' . $post->post_title . '</a></li>';
    
        restore_current_blog();
    }
    $html .= '</ul>';
    

    where $recent_across_network is a list of recent posts from across a multisite network, cached using the Transients API. There’s a related tutorial here.

    You could equally do:

    $postURI = get_blog_permalink( $post->blog_id, $post->ID );
    

    instead of switch_to_blog() / restore_current_blog() – not sure which is more efficient?