Created
April 14, 2020 12:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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