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 small problem with a code that works most of the time. I just discovered it cannot be used when I bulk edit custom post type. If I'm on individual post page and hit publish, the custom post type gets published and the user meta gets updated.

If I use the bulk edit to publish many posts at once, the user meta is not updated.

    function sa_ads_count() {
        global $wpdb, $post;
        $authordata = get_userdata($post->post_author);
        $author = $authordata->ID;
        // get initial count
        $single = true;
        $ads_count = get_user_meta( $author, 'ads_count', $single );
        if ( strlen( $ads_count ) > 0 ) {
            $ads_count = intval( $ads_count ); // make sure it's a number
            $ads_count++; // increment by one
        } else {
            // meta isn't set, so we'll create it
            $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", $author ) );

$stats = array();
foreach ( $rows as $row )
    $stats[ $row->post_status ] = $row->count;
            $ads_count = isset($stats['publish']) ? $stats['publish'] : 0;
        }

        update_user_meta( $author, 'ads_count', $ads_count );
    }

add_action( 'pending_to_publish', 'sa_ads_count' );

Any idea what is wrong here? Is it a WordPress limitation or this can be solved? The thing is, I need this hook as I only want to count and store for every user, the number of posts that I approve and publish.

Later on, I want to change new post status directly to publish based on the number of posts the user has approved. This I already achieved.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Try using the $post parameter:

add_action( 'pending_to_publish', 'sa_ads_count' );

function sa_ads_count( $post )
{
    // use the $post variable here, not the missing global.
}
share|improve this answer
    
Thanks a lot toscho for opening my eyes. I got it to work with this final code: add_action( 'transition_post_status', 'sa_ads_count', 10, 3 ); function sa_ads_count( $new_status, $old_status, $post ) { if ( $new_status == 'publish' && $old_status == 'pending' ) { // the post has been approved, do stuff ... } –  ovidiubica Apr 19 '13 at 18:10
    
@ovidiubica See my update, it is even easier. For more see this answer. –  toscho Apr 19 '13 at 18:26
    
Too bad I can't vote for now, this is even better, I can't believe how easy this was. Little details make big difference. Thanks a lot Thomas. –  ovidiubica Apr 19 '13 at 19:27
    
One more question, I was hoping to only count and store pending_to_publish actions but it seems this also counts new_to_publish actions. Could this be due to the fact that users are "contributors" instead of "authors" ? –  ovidiubica Apr 20 '13 at 6:42
    
Hm, that shouldn’t happen. I think it worth a new question. –  toscho Apr 20 '13 at 8:16

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.