Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Add custom field automatically (add_post_meta) with value based on number of words of article

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 ) );
}

Related Posts:

  1. Update all posts automatically when using post_meta
  2. Better post meta efficiency?
  3. Change post format using custom field
  4. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  5. What Is meta_id In wp_postmeta?
  6. How can I sort posts by the date and a custom meta field?
  7. Cannot retrieve a custom RSS field from posts
  8. Customize rel=canonical tag for single blog post
  9. How to I retrieve the ID from the Posts page?
  10. if in category but only with post meta
  11. Saving Post Data in Another Database
  12. Add multiple meta keys to a post at once
  13. saving/reading custom field value does not work – no value gets POSTed
  14. Post meta not working
  15. How do you update post date (year only) in a separate custom field?
  16. WP_Query sort by meta_value_num or date
  17. Creating Ordered Query using Meta_key
  18. How enter custom post meta or custom field on box post list archive page?
  19. How to update custom fields using the wp_insert_post() function?
  20. How to check if post meta key exists or not in wordpress database
  21. How to add category to: ‘wp-admin/post-new.php’?
  22. How to protect pages with double authentication: password + email (in custom field)
  23. How set featured posts using checkbox in post edit screen?
  24. How can I retrieve multiple get_post_meta values efficiently?
  25. Code to make a post sticky
  26. How to allow hidden custom fields to be added from wp-admin/post.php?
  27. Add a checkbox to post screen that adds a class to the title
  28. Custom Field in Featured image for A particular post
  29. Export WordPress Posts and Meta Information in CSV format
  30. Delete duplicated wp_postmeta record
  31. Convert custom fields to post categories
  32. Will a large postmeta table slow a site down?
  33. How to batch update post content with custom post meta value
  34. Change slug with custom field
  35. I am trying to create a simple frontend form for posting
  36. Prevent duplicate posts in wp_insert_post using custom fields
  37. HTML code in Custom field
  38. How to show posts rank based on custom field value
  39. Setting post meta data to random value during post status transition / on publish
  40. Add custom field to all posts in specific category
  41. Adding a Nav menu to post admin
  42. meta_box or custom_field as a second tinymce post-instance?
  43. Edit meta data does’t work with custom sql
  44. Guest Author – How to modify my custom function code if the guest author URL will follow a particular pattern/format?
  45. Create A Metabox For A Custom Field
  46. Paragraphs removed when using get_post_custom()?
  47. Human Time Diff, change mins to minutes
  48. get_post_custom stripping styling issue
  49. Does WP get all post_meta on POST page?
  50. Populating a page with content from post custom fields
  51. How to change what the post creation page looks like?
  52. How can I make existing custom fields easier to edit/add in my theme?
  53. Exporting Data from WordPress into a flat table
  54. Adding Multiple Values to a Post Meta Key
  55. Add custom fields to post programmatically
  56. How to get Advanced Custom Field Value According using POST ID? [closed]
  57. Using radio button meta data from a custom meta box
  58. Autoremove empty custom fields
  59. How to add a custom field after wp post
  60. Custom fields on a virtual page
  61. PHP Notice error (when on 404 page)
  62. Get a custom field of all posts on current blog page
  63. Only display posts after current date
  64. Checking if a post with certain meta value exists
  65. How to permanently delete a post meta entry?
  66. Global $post not working in OOP function WordPress
  67. How can I get a post field value using javascript?
  68. Automatic value for custom fields for posts
  69. How to generate numbers indistinguishable for the IDs of the posts
  70. Modify WP_Post before processing
  71. Understanding and using metaboxes in posts
  72. Save re-arranged draggable post items to wordpress database
  73. I would like to give special promotion for the first 100 posts in my blog? Can anyone tell me how to do that?
  74. Change post_date to post_modified date on post template?
  75. Duplicating Table of Contents for Paginated Post
  76. Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
  77. Guest Author – How to display posts on /author/ archive page
  78. WP_Query orderby not working with meta_value_num and menu_order
  79. is there a way to show the the post title after the image?
  80. Get Meta Key Value While Saving Post
  81. How do I correctly set up a WP-Query to only show upcoming event-posts?
  82. How to display Common posts from specific Tag & Category with Shortcode
  83. Update Post Meta for a logged in user
  84. Two Custom Post Types Many to Many Relationship
  85. Force the “Choose from the most used tags” meta box section to always be expanded
  86. Automatically add custom fields value to wordpress post
  87. Meta value does not save for scheduled posts
  88. Custom Posts Query and meta_query Sort Order
  89. Is there a way to notify specific users when new posts are published to specific pages?
  90. MySql query to get posts with all meta and terms
  91. How to compare two posts including their meta fields on a scalable base?
  92. Categories list into registration form
  93. Getting value from get_post_custom
  94. Add a custom meta box in the post options that loads some html code in the header
  95. Calling Different Custom Post Timestamps in a table
  96. Is it possible to paste a link without tags and make it directly a link in a post?
  97. Query Posts With Over 1000 Views
  98. Saving custom fields to a custom taxonomy
  99. Hooking into the post editing screen for an existing page only
  100. Query Posts by Custom Field
Categories posts Tags custom-field, post-meta, posts
Is it possible to get the currently logged in admin’s IP?
Display custom post type in template

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress