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

Follow me!

If you like this article, go ahead and follow me on Twitter:

Follow @getButterfly

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. Last modified on Monday, July 16, 2018.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Policy