• Resolved Troy Chaplin

    (@areziaal)


    I’m trying to run a loop on my homepage where 3 random user profiles will be displayed. I found the following in the forums, which works almost exactly how I want: https://www.ads-software.com/support/topic/display-random-authors-on-homepage?replies=16

    However, I’m not quite sure how I would edit this to exclude any user profiles that are set as the administrator role.

    Any help from the community would be greatly appreciated. My code is as follows:

    <?php
    $uid = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_status = 0");
    shuffle($uid);
    array_rand($uid);
    
    for($someNumber = 1; $someNumber <= 3; $someNumber++) {
    
    $userID = $uid[$someNumber]; ?>
    
    	<article>
    
            <h1><a href="<?php echo get_author_posts_url($userID); ?>" class="author"><?php the_author_meta( 'display_name', $userID ) ?></a></h1>
    
            <div class="postmeta"><a href="<?php echo get_author_posts_url($userID); ?>" class="author">View Author Bio</a></div>
    
    	</article>
    
    <?php } unset($uid,$userID); ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • I think you can get what you want by changing the query to this:

    $uid = $wpdb->get_col("SELECT ID
    FROM $wpdb->users u, $wpdb->usermeta m
    WHERE user_status = 0
    AND u.ID = m.user_id
    AND m.meta_key = 'wp_capabilities'
    AND m.meta_value NOT LIKE 'administrator'");

    Thread Starter Troy Chaplin

    (@areziaal)

    Thanks, but unfortunately this causes the 3 users being listed to all be the only administrator account for the site, none of the others are being included in the query.

    Sorry, I didn’t cut and paste the query I tested, and I should have. I made an error in retyping it. Please try this:

    $uid = $wpdb->get_col("SELECT ID
    FROM $wpdb->users u, $wpdb->usermeta m
    WHERE user_status = 0
    AND u.ID = m.user_id
    AND m.meta_key = 'wp_capabilities'
    AND m.meta_value NOT LIKE '%administrator%'");
    Thread Starter Troy Chaplin

    (@areziaal)

    Unfortunately this has the same results as the first example you provided. My revised code, with old query commented out and your sample added in, looks like:

    <?php
    	// $uid = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_status = 0");
    
    	$uid = $wpdb->get_col("SELECT ID
    	FROM $wpdb->users u, $wpdb->usermeta m
    	WHERE user_status = 0
    	AND u.ID = m.user_id
    	AND m.meta_key = 'wp_capabilities'
    	AND m.meta_value NOT LIKE '%administrator%'");
    
    	shuffle($uid);
    	array_rand($uid);
    
    	for($someNumber = 1; $someNumber <= 3; $someNumber++) {
    	$userID = $uid[$someNumber];
    
    ?>

    I added meta_value so I could easily check the results of the query. Here is the exact query that I tested:

    SELECT ID,meta_value
    FROM wp_users u, wp_usermeta m
    	WHERE user_status = 0
    	AND u.ID = m.user_id
    	AND m.meta_key = 'wp_capabilities'
    	AND m.meta_value NOT LIKE '%administrator%'

    Don’t know why it works for me, but not for you.

    Thread Starter Troy Chaplin

    (@areziaal)

    Strange. Thanks for the help! I’ll keep looking for a solution, I also have another bit of similar code that works, except it only randomizes the first 3 people based on the alphabetical order of their last names, instead of randomizing from the entire list of users in the editor role:

    <?php
    
    	$args  = array(
    		'number' => 3,
    		// 'role' => 'editor',
    	);
    
    	global $wp_query;
    	$wp_query = new WP_User_Query($args);
    
    	// Get the results
    	$authors = $wp_query->get_results();
    
    	if ($authors) : shuffle ($authors);
    
    ?>
    
    <?php foreach ($authors as $author) : ?>
    
    <!-- DO STUFF -->
    
    <?php endforeach; ?>
    
    <?php endif; wp_reset_query(); ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Displaying random authors while excluding admin role’ is closed to new replies.