Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Any ideas?

share|improve this question
    
You could use some form of caching ( with Transients for instance codex.wordpress.org/Transients_API) to cache the results in between? I'd suggest to first write the function before dealing with performance. See also: secure.wikimedia.org/wikipedia/en/wiki/… –  BjornW Aug 23 '11 at 10:49
    
Thanks for that. Any idea on the performance issues caused by using switch_to_blog() ? –  Pippin Aug 23 '11 at 13:58
    
Hey @Pippin, did you ever get this sorted out? If so, it would be great to post your answer/findings here for others. Thanks! –  Matthew Boynes Feb 7 '12 at 23:21
    
No, a solution was never found. –  Pippin Feb 8 '12 at 2:50
add comment

2 Answers

That sounds pretty simple.

Use: http://codex.wordpress.org/Function_Reference/switch_to_blog

To go back use:

restore_current_blog();

This allows you to switch from site to site.

Then: http://codex.wordpress.org/Function_Reference/get_permalink

My assumptions are that you have both the Blog ID and the Post ID.

If that's the case - using those two functions should solve your problem.

share|improve this answer
add comment

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.