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 );
Related Posts:
- Is there a way getting post fields using `transition_post_status`
- How to check if post meta key exists or not in wordpress database
- How can I retrieve multiple get_post_meta values efficiently?
- Export WordPress Posts and Meta Information in CSV format
- Update all posts automatically when using post_meta
- How to batch update post content with custom post meta value
- Edit meta data does’t work with custom sql
- How to change post status from publish to draft using hook in wordpress?
- Hiding by default specific post status on backend list?
- Does WP get all post_meta on POST page?
- Custom Post Status Posts viewable to the public
- Sanitizing `wp_editor();` Values for Database, Edit, and Display
- Adding Multiple Values to a Post Meta Key
- How can a guest view the “Pending Review” Post?
- add filter to “quick edit menu” in wordpress admin
- PHP Notice error (when on 404 page)
- What Is meta_id In wp_postmeta?
- What differences are there between a Privately Published post and a Draft post?
- How to permanently delete a post meta entry?
- How to generate numbers indistinguishable for the IDs of the posts
- Modify WP_Post before processing
- Update post status from “publish” to “trash” for half of posts
- I would like to give special promotion for the first 100 posts in my blog? Can anyone tell me how to do that?
- Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
- how to remove dash (-) post status from post title on posts listing page wordpress
- How can I sort posts by the date and a custom meta field?
- Get Meta Key Value While Saving Post
- How to change post status from frontend?
- Force the “Choose from the most used tags” meta box section to always be expanded
- Update postmeta Parent when post_status child change
- Meta value does not save for scheduled posts
- Posts and Attachments with “Published” status in Search
- How to compare two posts including their meta fields on a scalable base?
- Getting value from get_post_custom
- Admin – create custom post status and display above table
- Is it possible to paste a link without tags and make it directly a link in a post?
- Restrict edit, but allow preview for custom post status
- WordPress capabilities on pending post status
- Cannot retrieve a custom RSS field from posts
- How to automate featured posts number? [duplicate]
- why there are so many posts whoes post_type is revision? will these records waste too much database space?
- Customize rel=canonical tag for single blog post
- $post->post_type not working
- How to save meta checkbox WordPress
- how can i allow users to view their own pending posts in a front-end page
- How to calculate the average of a post meta value(Numeric) of a specific author
- wpColorPicker – problem with implementation to post meta
- Change post author without using wp_update_post()
- if in category but only with post meta
- wp_query with ajax
- Update post meta within save_post action
- Updating post meta for checkbox
- Saving Post Data in Another Database
- Custom permalink for each post
- How can I tell if a post has been published at least once?
- Automatic blog page with custom design [closed]
- What function publishes a post when you click the publish button?
- Retrieve data from post meta meta_key having ‘_product_fq_image’ while having author_id and post type is PRODUCT
- Post Status Frontend Announcement
- Adding a meta box to determine the sidebar [closed]
- The loop starting at a certain ID
- Display amount of pending posts in back/front end
- Simple Custom Metabox Not Saving
- Get post category as a separate string and url
- Showing posts for today and future
- Show Last Revision Of Post That Has A Published Status On Single Page
- Featured Images most often doesnt appear
- Post meta not working
- How to bulk Update URLs to new values?
- SQL DELETE multiple post_meta on single and multi-sites
- Updating post status via rest api
- Private post caught in endless redirect loop
- How to retrieve all meta data directly from the $post object? Such as $post->related_topics?
- What is proper filter or hook action to add data to the $post object?
- How publish post from pending status
- Hide or remove custom post status
- Put page on Archived statut after end of publication date
- get_next_post() not working with future post status
- Display Updated Date Instead of Published Date
- How do you update post date (year only) in a separate custom field?
- Bulk Post update_post_meta
- Re-order Category Meta-data
- How to hide meta from search result only on pages?
- WP_Query sort by meta_value_num or date
- alt of attached image in post / pages
- Creating Ordered Query using Meta_key
- Best Way to detect unique posts in wp rest api
- post_name is not stored until post is published… where should I store my slug until it goes live then?
- Show posts between two Custom Fields date
- Add custom class to existing menu items from custom meta
- How to get only current images of a post
- Ordering posts alphabetically by meta keys and title
- Where should get_post_meta() go to get $post->ID get_post_meta() is empty and $post->ID & get_the_id() are working
- Adding custom fields to the Quick Edit screen – puzzled about the column concept
- get_posts includes “auto drafts”?
- If meta_key exists then don’t select this post?
- Post Format Status [closed]
- Do anything on post_status change [duplicate]
- Site ‘Categories’: save an admin global setting with post metadata [closed]
- Translation related post text to Arabic