Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 
WHERE ID NOT IN (1)
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
add comment

1 Answer

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

<?php
    $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
show 7 more comments

Your Answer

 
discard

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.