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 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?

share|improve this question
    
Store it for how long? –  vancoder Apr 18 '13 at 19:48
    
I want to store it indefinitely. Of course, the number has to increase when new posts are published. –  ovidiubica Apr 18 '13 at 19:54

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 Apr 19 '13 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 Apr 19 '13 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 Apr 19 '13 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 Apr 19 '13 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 Apr 19 '13 at 14:10

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.