0

I want to count author posts and store this number for later use even if some of the posts have been deleted in the meantime.

$rows = $wpdb->get_results( $wpdb->prepare( "SELECT post_status, COUNT(ID) as count FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post_type' GROUP BY post_status", $current_user->ID ) );
$stats = array();
foreach ( $rows as $row )
    $stats[ $row->post_status ] = $row->count;

        $post_count_published = isset($stats['publish']) ? $stats['publish'] : 0;

Any idea how this can be achieved?

2
  • Store it for how long?
    – vancoder
    Commented Apr 18, 2013 at 19:48
  • I want to store it indefinitely. Of course, the number has to increase when new posts are published.
    – ovidiubica
    Commented Apr 18, 2013 at 19:54

1 Answer 1

1

You can use update_user_meta() to add information to a user's meta fields, and count_user_posts() to get an initial count.

So, for instance:

<?php
    add_action( 'new_to_publish', 'wpse96358_author_count' );
    add_action( 'draft_to_publish', 'wps396358_author_count' );

    function wpse96358_author_count() {
        global $post;
        // get initial count
        $single = true;
        $author_count = get_user_meta( $post->post_author, 'author_count', $single );
        if ( strlen( $author_count ) > 0 ) {
            $author_count = intval( $author_count ); // make sure it's a number
            $author_count++; // increment by one
        } else {
            // the meta information isn't set, so we'll create it
            $author_count = count_user_posts( $post->post_author );
        }

        update_user_meta( $post->post_author, 'author_count', $author_count );
    }

?>

NB

You would be wise to consult the Action Reference and the Post Status Transitions reference to see which action would work best for you. I picked new_to_publish and draft_to_publish but it depends on when you want the updates to happen.

5
  • Thanks Pat, I'll give this a try. I rather go with pending_to_publish as I only want to count and store the ones I approve. Upon this, I want to change post status to published for new posts the users submit based on the number of previously approved posts. So, if post count is lower than 2, they go pending, else, they go live. I already achieved this but I have problems with posts that expire or the ones I delete. Those cannot be counted anymore.
    – ovidiubica
    Commented Apr 19, 2013 at 3:41
  • One more question, count_user_posts does not count custom post types, am I correct? I still have to dive into the database.
    – ovidiubica
    Commented Apr 19, 2013 at 3:47
  • count_user_posts() doesn't yet support custom post types, according to the Codex page linked in my answer, but see the note on adding post type support.
    – Pat J
    Commented Apr 19, 2013 at 12:41
  • Thanks Pat, your code works fine, I just needed to pull author id outside the loop... $authordata = get_userdata($post->post_author); $author = $authordata->ID; I do have a rather strange problem though. If I bulk edit/publish the posts, the user meta doesn't get updated. If I publish them individually, it does. Any idea where the problem is? Bulk edit doesn't go through pending_to_publish ?
    – ovidiubica
    Commented Apr 19, 2013 at 14:01
  • I'm not sure. From your experience, it sure sounds like bulk edit bypasses the post transition hooks, or at least the pending_to_publish one. That might actually be grounds for a new question. (Also: If my answer worked for you, would you mind marking it as accepted? Thanks.)
    – Pat J
    Commented Apr 19, 2013 at 14:10

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.