WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
What is $row->ID, it's the user's ID right? – Rutwick Gangurde Oct 5 '11 at 9:37
$row->ID means user's ID ofcourse. – syed Oct 5 '11 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! – Rutwick Gangurde Oct 5 '11 at 10:18

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!

share|improve this answer
Parse error: syntax error, unexpected T_DOUBLE_ARROW on line 85 – syed Oct 5 '11 at 10:12
line-85 contains this : 'cat' => -60, – syed Oct 5 '11 at 10:12
Did you remove the comments? And one of those lines? – Rutwick Gangurde Oct 5 '11 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 Oct 5 '11 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/… – Rutwick Gangurde Oct 5 '11 at 10:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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