ImagePress: How to get random authors with at least one published image

ImagePress Author Discovery
Photo by Shane Hauser on Unsplash

This small function allows you to get random authors with at least one published image, excluding the ones you follow. This is a great feature for author discovery and new follows.

This feature works with ImagePress version 7 or higher.

add_action('pre_user_query', function($query) {
    if ('rand' === $query->query_vars['orderby']) {
        $query->query_orderby = str_replace('user_login', 'RAND()', $query->query_orderby);
    }
});

function get_random_feed_authors($number, $currentUserId) {
    $myFollowers = array(pwuf_get_following($currentUserId));
    $myFollowers = array_unique($myFollowers);
    $myFollowersToString = implode(',', $myFollowers[0]);

    $users = get_users(array(
        'fields' => array('ID', 'display_name'),
        'orderby' => 'rand',
        'number' => $number,
        'who' => 'authors',
        'exclude' => $myFollowers[0],
        'has_published_posts' => get_post_types(array('public' => true)),
        // 'query_id' => 'authors_with_posts', // not used anymore
    ));

    //shuffle($users); // optional

    return $users;
}

Use this in your theme’s functions.php file or in a custom plugin.

Added by Ciprian on Monday, July 16, 2018 in Blog, ImagePress

Unlimited Automated Page Speed Monitoring & Tracking. Completely free.
Use SpeedFactor to track your website. It’s simple, reliable, and best of all, it’s free forever.
See how real people experience the speed of your website. Then find (and fix) your web performance problems.
Get Started

Related Articles


Privacy Policy