0

This script list users with their last post. How to exclude some authors from guery ?

<?php

    //List of users sorted descending by date of lastest post written by user
    $uc=array();
    $blogusers = get_users_of_blog();

    if ($blogusers) 
    {
        foreach ($blogusers as $bloguser) 
        {
            $user = new WP_User( $bloguser->user_id );
            if ( !empty( $user->roles ) && is_array( $user->roles ) )
            {
                $userpost = get_posts('showposts=1&author='.$bloguser->user_id);
                $uc[$bloguser->user_id] = '';
                if ($userpost) 
                {
                    $uc[$bloguser->user_id]=$userpost[0]->post_date;
                }
            }
        }
        arsort($uc);
        $i = 0;
        foreach ($uc as $key => $value) 
        {
            $user = get_userdata($key);
            $post_count = count_user_posts($user->ID);
            if ($post_count && $i < 10) 
            {
                $author_posts_url = get_author_posts_url($key);

                //preparing query for titles
                $args=array(
                'author' => $user->ID ,
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'ignore_sticky_posts'=> 1,
                );
                $my_query = null;
                $my_query = new WP_Query($args);

                if( $my_query->have_posts() ) 
                {
                    //echo 'List of Posts for ' . user->user_firstname . ' ' . $user->user_lastname;
                    while ($my_query->have_posts()) : $my_query->the_post(); 

?>
1

Hey use this to grab all users excluding users with ID - 1,2,4

$blogusers = get_users(array('exclude' => array(1,2,4)));

Note -

The function get_users_of_blog() is depricated, so it'd be good idea to use get_users() function which supports excludeing of users by their IDs.

  • you're using get_users_of_blog to get the list of users, then you should exclude some users right in that function. that's idea. – amit Aug 14 '12 at 6:27
1

To exclude authors you add a minus sign in front of the user id. So in your case it could look like this:

$args=array(
    'author' => '-2, -3, -4', //excludes users with id 2, 3 and 4.
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'ignore_sticky_posts'=> 1,
);

Read more in the codex

  • Thank you for answer,but It is not work. – Ronin Aug 13 '12 at 14:54
  • Hmm yeah, that's right. Including AND excluding in the same query doesn't really make sense. Remove $user->ID from the author and use only exclude. I'll edit my post. – pbd Aug 13 '12 at 15:10
  • Thank you again. But it is does not work too ) – Ronin Aug 13 '12 at 15:46
  • Alright, try changing it to an array of the authors you want to exclude. I'll edit again. – pbd Aug 13 '12 at 16:29
  • there is an error - Warning: urldecode() expects parameter 1 to be string, array given in /home/z/public_html/wp-includes/query.php on line 2275 – Ronin Aug 13 '12 at 19:15

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.