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.

1 Answer 1


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.
  • 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, 2013 at 18:10
  • @ovidiubica See my update, it is even easier. For more see this answer.
    – fuxia
    Apr 19, 2013 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, 2013 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, 2013 at 6:42
  • Hm, that shouldn’t happen. I think it worth a new question.
    – fuxia
    Apr 20, 2013 at 8:16

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.