Skip to content

Instantly share code, notes, and snippets.

@roytanck
Created April 14, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roytanck/4c1df320c0d58a9ad7e0bba06acb22e5 to your computer and use it in GitHub Desktop.
Save roytanck/4c1df320c0d58a9ad7e0bba06acb22e5 to your computer and use it in GitHub Desktop.
Find out if a user has published content on a multisite WordPress install.
/**
* Find out if a user has published content anywhere on the (multi-)site
*/
function rt_user_has_published_content( $user_id ) {
// Arguments for get_posts, requesting 1 post of any type.
$args = array(
'post_type' => 'any',
'posts_per_page' => 1,
'author' => $user_id,
'fields' => 'ids',
);
// Distinguish between multisite and single site.
if( is_multisite() ){
// Get all subsites where the user has a role.
$user_subsites = get_blogs_of_user( $user_id );
// Return false if the user does not have any roles at all.
if( empty( $user_subsites ) ){
return false;
}
// Loop through the subsites to check for content, return true is any is found.
foreach( $user_subsites as $key => $subsite ){
$post_count = 0;
switch_to_blog( $key );
$user_posts = get_posts( $args );
$post_count = count( $user_posts );
restore_current_blog();
if( $post_count > 0 ){
return true;
}
}
// No content found.
return false;
} else {
$user_posts = get_posts( $args );
$post_count = count( $user_posts );
if( $post_count > 0 ){
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment