Using WordPress ‘get_blog_permalink()’ PHP function

The get_blog_permalink() WordPress PHP function retrieves the permalink for a post on another blog within a multisite network.

Usage

get_blog_permalink($blog_id, $post_id);

Custom example:

Input: get_blog_permalink(3, 6);

Output: The permalink for the post with ID 6 on blog ID 3.

Parameters

  • $blog_id (int) – Required. The ID of the source blog.
  • $post_id (int) – Required. The ID of the desired post.

More information

See WordPress Developer Resources: get_blog_permalink()

Examples

Display the permalink for a specific post on another blog

This example retrieves the permalink for post ID 42 on blog ID 2 and displays it.

// Get the permalink
$permalink = get_blog_permalink(2, 42);

// Display the permalink
echo "The permalink for post ID 42 on blog ID 2 is: " . $permalink;

Create a hyperlink to a post on another blog

This example creates a hyperlink to post ID 10 on blog ID 5.

// Get the permalink
$permalink = get_blog_permalink(5, 10);

// Create a hyperlink
echo '<a href="' . $permalink . '">Visit post ID 10 on blog ID 5</a>';

This example displays a list of permalinks for posts with IDs 1, 2, and 3 on blog ID 7.

// Post IDs to retrieve
$post_ids = array(1, 2, 3);

echo "<ul>";
foreach ($post_ids as $post_id) {
    $permalink = get_blog_permalink(7, $post_id);
    echo "<li><a href='" . $permalink . "'>Post ID " . $post_id . " on blog ID 7</a></li>";
}
echo "</ul>";

This example displays a list of permalinks for the 5 latest posts on blog ID 12.

// Switch to the target blog
switch_to_blog(12);

// Get the 5 latest posts
$latest_posts = get_posts(array('numberposts' => 5));

// Switch back to the original blog
restore_current_blog();

// Display the permalinks
echo "<ul>";
foreach ($latest_posts as $post) {
    $permalink = get_blog_permalink(12, $post->ID);
    echo "<li><a href='" . $permalink . "'>" . $post->post_title . "</a></li>";
}
echo "</ul>";

This example displays a list of permalinks for posts with the tag “travel” on blog ID 8.

// Switch to the target blog
switch_to_blog(8);

// Get the posts with the "travel" tag
$travel_posts = get_posts(array('tag' => 'travel'));

// Switch back to the original blog
restore_current_blog();

// Display the permalinks
echo "<ul>";
foreach ($travel_posts as $post) {
    $permalink = get_blog_permalink(8, $post->ID);
    echo "<li><a href='" . $permalink . "'>" . $post->post_title . "</a></li>";
}
echo "</ul>";