1

I am re-using some old code on a new website where I show all users in a listing with last post image as background. This all works fine (I know the get_users_of_blog is deprecated, but when I use get_users my code breaks)...

<?php
$blogusers = get_users_of_blog();
if ($blogusers) { 
  foreach ($blogusers as $bloguser) { 
    $args = array( 
      'author' => $bloguser->user_id, 
      'showposts' => 1,
      'caller_get_posts' => 1
    ); 
    $my_query = new WP_Query($args); 

    if( $my_query->have_posts() ) { 
      while ($my_query->have_posts()) : $my_query->the_post(); ?>  
        ...........
      <?php endwhile; 
    } 
  } 
} 
?> 

The thing is I can't get it ordered by name and exclude authors in the list. I tried doing this

$blogusers = get_users_of_blog(array('role' => 'editors', 'orderby => ID));

But this is not doing a darn thing. Anyone? Thank you!

2

Yes, the get_users_of_blog() function is deprecated, and the reason your code breaks after using get_users() is the ->user_id part, you should use ->ID instead.

$blogusers = get_users();

if ($blogusers) { 

  foreach ($blogusers as $bloguser) { 

    $args = array( 
      'author'           => $bloguser->ID, 
      'showposts'        => 1,
    ); 

    $my_query = new WP_Query($args); 

    if( $my_query->have_posts() ) { 
      while ($my_query->have_posts()) : $my_query->the_post(); ?>  
        ...........
      <?php endwhile; 
    } 

  } 

} 

If you are on a multisite, you should set the blog_id argument of get_users to your blog's ID.

Now, about excluding the authors. If you take a look at the author parameters of WP_Query(), there's a author__not_in argument that accepts an array of author IDs. So, to exclude the test role from your query, set the role__in or role argument of get_users() to test, and then use the results in your WP_Query(). You didn't provide accurate details so I'm not sure what exactly you are looking after, but these should do the trick.

  • 1
    Thank you very much! I used this to exclude authors from the get_users $blogusers = get_users(array( 'role' => 'editor' )); And works like a charm! Thanks again! – AKNL Mar 5 '18 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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