I have a magazine wordpress site in which I have designed a separate page to show all the authors with their nice_names (with URI) and no. of posts. the code is like this:

$current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;

$rows = $wpdb->get_results("SELECT ID, display_name from $wpdb->users 
ORDER BY display_name");
$author_ids = $wpdb->get_results($query);
global $wp_rewrite;

for ($i=$start;$i < $end ;++$i ) {
   $row = $rows[$i];
 $curauth = get_userdata($row->ID);
 $user_link = get_author_posts_url($curauth->ID);
 $post_count = get_usernumposts($curauth->ID);

To show the number of posts each author has, I want to show like this:

  • showing number of all posts but excluding a certain category (ID=60)
  • showing number of posts in a single category (ID=60)

How do I have to modify the above code?

  • What is $row->ID, it's the user's ID right? Commented Oct 5, 2011 at 9:37
  • $row->ID means user's ID ofcourse.
    – syed
    Commented Oct 5, 2011 at 10:14
  • If $row->ID is the user ID, then why are you getting the entire userdata array, just for getting the ID again? Use it directly! Commented Oct 5, 2011 at 10:18

1 Answer 1


Ahh friend, you don't need such a hard way! This should do...

    $curauth = get_userdata($row->ID);
    $user_link = get_author_posts_url($curauth->ID);
    $posts = get_posts(array(
                             'author' => $curauth->ID,
                             'cat' => 60, //for single category, 60
                             'cat' => -60, //for excluding this category
                             'posts_per_page' => -1,
    $post_count = count($posts);

Remove the commented line as per your requirement. Try and let me know!

  • Parse error: syntax error, unexpected T_DOUBLE_ARROW on line 85
    – syed
    Commented Oct 5, 2011 at 10:12
  • line-85 contains this : 'cat' => -60,
    – syed
    Commented Oct 5, 2011 at 10:12
  • Did you remove the comments? And one of those lines? Commented Oct 5, 2011 at 10:16
  • I have cleared this error by modifying the line as 'author' => $curauth->ID, ... but the problem is , the result (after deducting cat=60 posts) is showing for some authors as "5" only where as they have more than 5-posts.
    – syed
    Commented Oct 5, 2011 at 10:31
  • That is what 'posts_per_page' is for... The default number returned is 5, but if you set 'posts_per_page' as -1, it will return all posts. codex.wordpress.org/Function_Reference/… Commented Oct 5, 2011 at 10:49

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.