You can automatically add a custom field to each new post on publish (on change status to publish). And then set it’s value to a random number between 829 and 1013.
Here is the function that will do just that.
// Create custom field on post publish
function wpse_custom_field_on_publish( $new, $old, $post ) {
if ( $new == 'publish' && $old != 'publish' && !get_post_meta( $post->ID, 'post_views', true ) ) {
add_post_meta( $post->ID, 'post_views', rand(829, 1013), true );
}
}
add_action( 'transition_post_status', 'wpse_custom_field_on_publish', 10, 3 );
In above code, post_views
is the name of the custom field that we are using to count post views. You should change it to yours before using it in your theme.
Just FYI, this will go in functions.php
file.
Related Posts:
- Hide the post count behind Post Views (Remove All, Published and Trashed) in Custom Post Type
- Get total views of all posts by author
- C++ – how to find the length of an integer
- How can I make a PHP counter?
- Counting the posts of a loop (WP_Query)?
- Category and children post count
- How to display 2 equal columns of li based on the count of items in wp_nav_menu divided by 2?
- Show weekly posts statistic in WordPress
- Count number of posts by author in a category
- How we count the user draft posts
- Double count view in archive.php
- What is the best way to count and display the number of posts?
- How can I count post views of REST API calls and update them in an ACF field?
- Get count of terms with a post and another taxonomy term?
- How can I get global $post to work for CPT and update user?
- Get Gravity Forms field values and count how many? [closed]
- Counting posts in custom post type by author
- Show ellipsis (…) only if the number of characters exceeds limit defined in substr
- Post Views / Hit Counter Problem?
- Count post views in loop
- Count post + add number =
- How to do simple addition to increase favorites count of my wordpress posts
- Count posts published in one particular day: the lighter way
- What is the best way to do this? [closed]
- Search Count WordPress Theme
- How to read out the excerpt length (for if-condition)
- $count_posts->draft & published
- Count the total views of all user posts published
- Author.php Page views counter
- CountPost WordPress Custom Taxonomy
- get the count of table rows
- How to calculate posts number? [closed]
- How make animation increase of number
- Having problems getting the number of plays in a WordPress Playlist from an audio file
- How to show the amount of post that have on the site?
- Counting the number of elements with the values of x in a vector
- How can I count the occurrences of a list item?
- Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
- Count how many files in directory PHP
- select * from table where column = something or, when unavailable, column = something else
- count posts from custom taxonomy terms by year
- Comment Count for each Comment Author
- Display user’s total comment count outside The Loop
- Creating a custom MCE view for your shortcodes [closed]
- Count posts that have specific taxonomy term attached
- Count number of post in Taxonomy?
- User Published Post Count
- How to use global post counter in the loop?
- Show that current post is number X out of X
- Counting pageviews on high-traffic cached sites
- Change post category based on latest posts views
- How to get the number of child categories a specific parent category has?
- How to add “time” data this?
- get_queried_object error How to show post count by month in the taxonomy page
- wp_count_posts, wp_count_terms and wp_count_comments for specific user?
- How to count custom post types with conditional operators
- Count posts with specific term_meta
- How to count rows of table in the_content()
- How to improve post views count display?
- User’s total comment count excluding comments on their own posts
- Post count by month of taxonmy term
- custom post type category count shortcode
- Counting posts with argument without retrieving the posts
- Show posts count for Categories and Tags in wp_nav_menu
- How to add category post count in main navigation menu [closed]
- How to show the number of website visitors in my theme
- How to count the number of archives there are
- How can i count post from my post type taxonomy
- How can i count the post added in relationship field, theme display
- Show the amount of posts in a tag in a specific category has
- How can I count ACF sub_field with a certain value
- How do I insert a after every 5 posts in a WordPress Loop that infinitely loads posts?
- Get Author Count By Day, Week and Month
- How to count other posts not having specific taxonomy terms?
- counting trackbacks, pingbacks and comments of a post
- How to Define Custom Number of Items in Comment Feed
- Order terms by count – missing terms
- count the number of views of a post excluding own views
- Count user posts and store the number for later use
- How include comment and post count of user in this custom query
- How to reset post views count
- Allow a specific user role I have created to only upload one image to his media library
- Author post count in category
- get_queried_object not work in taxonomy page
- Show post count in custom taxonomy page
- How can i show post views using specified post ID?
- How Can I add show_post_count in get_archives_link?
- Modify the category post counting function
- Implementation to count page visits of unique visitors based on a cookie
- Add subcategories posts to the counts column at the admin’s categories list
- count_posts to a standalone link
- How can I make post view count editable? [closed]
- divide custom field values in div every two values
- How to mark every 3rd post
- How to add a post counter to the list of custom taxonomy terms?
- how to localize the number of wordpress post views?
- How to limit the number of posts a user can publish based on user type
- How to add together (get the sum) a field from all author’s posts
- Counting number of identical meta keys
- How do I count the number of pages a user has seen on my site, and force them to log in after a certain amount