Upon publishing an unpublished draft, I'd like the custom meta value "post views count" to be set to a random variable between 829 and 1013.

I am using the code below, which doesn't always work. It works when I create a test draft and publish it immediately, but not when I save a post for a few days or assign it to another writer it doesn't work.

NOTE: I don't want to create a new custom field. I just want to update the existing field, which is called post_views_count.

function wpse_custom_field_on_publish( $new, $old, $post ) {
  if ( $new == 'publish' && $old != 'publish' && !get_post_meta( $post->ID, 'post_views_count', true ) ) {
    update_post_meta( $post->ID, 'post_views_count', rand(829, 1013), true );
add_action( 'transition_post_status', 'wpse_custom_field_on_publish', 10, 3 );
  • 1
    What do you mean in "it doesn't always work", please be more specific. sidenote, you probably don't use update_post_meta correctly, not sure if it is related. – Mark Kaplun Nov 13 '14 at 13:28
  • I mean if I create a post right now and publish it immediately, the code works, and the views get a random number per the above. If I create a post and save as a draft, assign it to a different user/editor, then save for review, then publish, it gets published with a view count of 0. That's what I meant by "it doesn't work." I don't want any posts to be published with a view count of 0. Should I use add_post_meta, or where do you think the issue is? I am also open to entirely new code. – location Nov 13 '14 at 13:39
  • @location Additional info always as an edit, please. – kaiser Nov 13 '14 at 15:02

Slight change in your code,

function wpse_custom_field_on_publish( $new, $old, $post ) {
    // Only run on "from X > to publish"-transitions
    if ( $new === 'publish' && $old !== 'publish') { 
        $page_views = get_post_meta( $post->ID, 'post_views_count', true );
        // Only set 'post_views_count' post meta value if there is none so far
        if ( empty( $page_views ) )
             update_post_meta( $post->ID, 'post_views_count', rand( 829, 1013 ) ); 
add_action( 'transition_post_status', 'wpse_custom_field_on_publish', 10, 3 );
  • Just updated the code with yours, published a post that was saved pending review, but it didn't work. It published with a view count of 0. the post was never published previously. – location Nov 13 '14 at 13:50
  • 2
    Other code is affecting on your page counts, because I checked above code at my end, and it is working fine. – WisdmLabs Nov 13 '14 at 14:06
  • I tested too, and it does work, except in the drafts I have saved already. Such a frustrating problem. One last question: How would I go about making the Views field in admin editable? Thank you. – location Nov 13 '14 at 14:28
  • 1
    @location How could a code that runs on the transition_post_status hook, which runs during changes of the status of a post, affect anything that has no change? I added comments to the code so you better understand the logic behind it. – kaiser Nov 13 '14 at 15:03
  • 1
    @WisdmLabs You might want to consider FALSE return values when there is no post ID present and make sure that the code really runs. – kaiser Nov 13 '14 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.