Im looking for a simple code to list all users with the role spectator and if they have made an entry in the current week enter "yes" and if not "no"

1 Answer 1


If by "made an entry" you mean "has published at least 1 post", then following will do it (with the proviso given below):

$args = array (
    'role' => 'spectator',
    ) ;
$spectators = new WP_User_Query ($args) ;

$users_with_recent_posts = array () ;
foreach ($spectators->get_results () as $user) {
    $args = array (
        // if you are interested in posts with a different
        // post_type, then just change the following to
        // be the post_type you're interested in
        'post_type' => 'post',
        'date_query' => array (
            'after' => '7 days ago',
            'inclusive' => true,
        'author' => $user->ID,
        'posts_per_page' => 1,
        ) ;
    $posts = new WP_Query ($args) ;
    $users_with_recent_posts[$user->data->user_nicename] = $posts->found_posts > 0 ? 'yes' : 'no' ;

Proviso: the above will only show a user as yes if they have a least 1 post whose pust_status is currently publish, i.e., if a user published a post within the last 7 days but then that post had it's post_status changed to something other than publish they will be shown as no if that was the only post they published in the last 7 days.

If you mean something else by "made an entry", then please explain what you mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.