getButterfly
Resume

NAVIGATION

ATTRIBUTION

getButterfly
Dublin, Ireland

All Content Copyright ©2018
getButterfly

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.


Leave a Reply

Your email address will not be published. Required fields are marked *

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


My Battle Tested Recommendations

Jetpack
Dreamhost
WordPress.com
CodeCanyon
SEMrush

Disclaimer: These recommendations contain affiliate links.