I'm trying to get the primary blog URL of a user. What I have works to grab the details, but if there is more than one blog it just returns them all :(

$user_id = get_current_user_id();
$user_blogs = get_blogs_of_user( $user_id );
foreach ($user_blogs AS $user_blog) {
 echo $user_blog->siteurl;

How can I modify this to output the primary blog URL of the current user?

I'm not sure how to pull it from wp_usermeta primary_blog table, but that may do the trick.

What I would like is the URL from the user primary blog so when they login from the network main site and visit the account area, it shows links to various areas without directing them to the you attempted to access message first...

  • How do you define “primary” blog for an user? How do you recognize which one is primary? Apr 15 '19 at 5:54
  • core.trac.wordpress.org/browser/tags/5.1.1/src/wp-includes/… /** 27 * Get one of a user's active blogs 28 * 29 * Returns the user's primary blog, if they have one and 30 * it is active. If it's inactive, function returns another 31 * active blog of the user. If none are found, the user 32 * is added as a Subscriber to the Dashboard Blog and that blog 33 * is returned. 34 * 35 * since MU (3.0.0) 36 * 37 * param int $user_id The unique ID of the user 38 * return WP_Site|void The blog object 39 */ Apr 15 '19 at 6:05

There's actually a function for that: get_active_blog_for_user() https://developer.wordpress.org/reference/functions/get_active_blog_for_user/

$user_id = get_current_user_id();
$user_blog = get_active_blog_for_user( $user_id );
echo $user_blog->siteurl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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