WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In this topic our friend @toscho created a function that sets the font size according to the number of words in the post. Based on this function, I want to create a custom field whose value will be set depending on the number of words in the post. For example, if the post contains up to 200 words, automatically create custom field (add_post_meta) with value = 1; If your post contains more than 200 words, create custom field with value = 2.

Any idea is welcome. Thank you.

share|improve this question
up vote 2 down vote accepted

Hook into save_post, count the words, and update the post meta field.

Sample code, not tested:

add_action( 'save_post', 'wpse_73563_save_word_count', 10, 2 );

function wpse_73563_save_word_count( $post_ID, $post )
    if ( ! current_user_can( 'edit_' . $_POST['post_type'], $post_ID ) )
        return FALSE;

    $count = t5_word_count( $post->post_content );

    update_post_meta( $post_ID, '_word_count', ( 200 > $count ? 2 : 1 ) );
share|improve this answer
add_post_meta() will not work if the key already exists. – toscho Nov 28 '12 at 19:06
Great @toscho, instead of update_post_meta() I preferred using add_post_meta(). Thanks again. – BóbGCA Nov 28 '12 at 19:12

Your Answer


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.