1

Is there a way to count and show the count of all author posts in a custom post type except the current logged in user?

I was hoping there's a way to do it with count_many_users_posts but it doesn't appear there is a way to exclude authors, only include them.

I've done multiple searches but have found nothing to even hint at a right direction to head in.

Here's the codex example:

<?php
    $users = array(1, 3, 9, 10);
    $counts = count_many_users_posts($users);
    echo 'Posts made by user 3: ' . $counts[3];
?>

TIA!

ON EDIT:

Just wanted to let y'all know I haven't revisited this yet. I will try the solutions and come back when I get there. Another project popped up for the moment.

Thanks!

4 Answers 4

1

Inclusion and exclusion are often reversible. "Not this" (exclusive) is equal to "All of that, skipping this" (inclusive).

If in general the function does precisely what you need, you could just make a full list of user IDs and throwing the current one out.

1

If the number of users on your site, is relatively small, you could try:

$user_ids = get_users( 
    [ 
        'fields'    => 'ID',                     // Only return the user IDs
        'exclude'   => (array) get_current_user_id(),    // Exclude the current user
        'number'    => 10                        // Modify this number to your needs
    ] 
);

and then use this as an input into the count_many_users_posts() function:

$counts = count_many_users_posts( 
    (array) $user_ids,
    $post_type   = 'post', 
    $public_only = false 
);

where you can adjust the post type to your needs.

1

There are a number of WordPress query functions you could utilize, but I recommend keeping the query lean and avoid using a WP_Query since you don't need the post content. Here's a little function that will return the number of posts of post_type that are not owned by user_id:

function get_post_count_exclude_user($post_type,$user_id) {
    $query = "SELECT COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_author <> %s";
    return $wpdb->get_var( $wpdb->prepare( $query, $post_type, $user_id ) );
}

$number_of_posts = get_post_count_exclude_user('my_post_type',get_current_user_id());
0

I don't know why this works, but it does.

        $authorid = get_current_user_id();
        query_posts(array( 
            'post_type' => 'tasks',
            'author' => -$authorid,
        ) ); 
            $count = 0;
            while (have_posts()) : the_post(); 
                $count++; 
            endwhile;
            echo '<p class="else"><em>' .$count .'</em></p><span class="description">All other posts</span>';
        wp_reset_query();
3
  • Note that query_posts isn't recommended and you haven't defined the posts_per_page parameter so you might only be counting to maximum of 10 or whatever the default is on your site. This doesn't scale well for large number of posts (tasks). Imagine having tens of thousands of tasks ;-)
    – birgire
    Sep 10, 2015 at 16:06
  • I'm just shocked that it works at all. I wouldn't expect putting a minus sign in front of the variable would work but it does. I'll revisit on your suggestion though. Sep 10, 2015 at 17:51
  • yes that looks strange but should be ok ;-) With the fields parameter of WP_Query set to ids, it would reduce the amount of data fetched from the database. But you don't need the loop, you should be able to use the found_posts property instead.
    – birgire
    Sep 10, 2015 at 18:15

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.