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

how do I set a schedule event to modify all posts’s meta value weekly or monthly?

wp_schedule_event() is what you are looking for. With this function you can create a cron jon that WordPress will axecute on the specific interval you configure. This function should be called only on plugin activation and the scheduled event should be cleared on plugin deactivation. For example:

 //Create the weekly and monthly interval
 add_filter('cron_schedules', 'cyb_cron_schedules');
 function cyb_cron_schedules( $schedules ) {
     $schedules['weekly'] = array(
        'interval' => 604800, //that's how many seconds in a week, for the unix timestamp
         'display' => __('weekly')
     );
     $schedules['monthly'] = array(
        'interval' => 2592000, //that's how many seconds in a month (30 days), for the unix timestamp
         'display' => __('monthly')
     );
     return $schedules;
 }

 register_activation_hook( __FILE__, 'cyb_activation' );
 function cyb_activation() {
      //Unix timestamp for first run of the event. time() for now
      $firstrun = time();
      wp_schedule_event( $firstrun, 'weekly', 'cyb_clear_weekly_post_views' );
      wp_schedule_event( $firstrun, 'monthly', 'cyb_clear_monthly_post_views' );
 }

 register_deactivation_hook( __FILE__, 'cyb_deactivation' );
 function cyb_activation() {
      wp_clear_scheduled_hook( 'cyb_clear_weekly_post_views' );
      wp_clear_scheduled_hook( 'cyb_clear_monthly_post_views' );
 }

 function cyb_clear_weekly_post_views() {
      //Set post_views_count_weekly to 0. Doing this for all post may need a lot of resources
      $posts = get_posts(array('numberposts' => -1) );
      foreach($posts as $post) {
           update_post_meta($post->ID, 'post_views_count_weekly', 0);
      }
 }

 function cyb_clear_monthly_post_views() {
      //Set post_views_count_monthly to 0. Doing this for all post may need a lot of resources
      $posts = get_posts(array('numberposts' => -1) );
      foreach($posts as $post) {
           update_post_meta($post->ID, 'post_views_count_monthly', 0);
      }
 }

Note: if you need to run the scheduled event on a exact interval you should created a cron jon in your sever instead of using wp_schedule_event(). More info in wp_schedule_event().

Related Posts:

  1. How do I retrieve the slug of the current page?
  2. Most efficient way to get posts with postmeta
  3. Get posts by meta value
  4. Explanation of update_post_(meta/term)_cache
  5. How to extract data from a post meta serialized array?
  6. How to save an array with one metakey in postmeta?
  7. WordPress is stripping escape backslashes from JSON strings in post_meta
  8. How can I get the post ID from a WP_Query loop?
  9. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  10. How to update_post_meta value as array
  11. Adding meta tag without plugin
  12. What’s the point of get_post_meta’s $single param?
  13. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  14. How to edit a post meta data in a Gutenberg Block?
  15. Sanitizing integer input for update_post_meta
  16. post formats – how to switch meta boxes when changing format?
  17. Execute action after post is saved with all related post_meta records (data)
  18. Lack of composite indexes for meta tables
  19. Get a single post by a unique meta value
  20. if get_post_meta is empty do something
  21. How we get_post_meta without post id
  22. How get post id from meta value
  23. What is the code to get the download link for a product in WooCommerce?
  24. Safe to delete blank postmeta?
  25. advanced custom fields update_field for field type: Taxonomy
  26. update_post_meta not saving when value is zero
  27. Content hooks vs User hooks
  28. Meta compare with date (stored as string) not working
  29. Trying to get custom post meta through Jetpack JSON API [closed]
  30. How to update/insert custom field(post meta) data with wordpress REST API?
  31. Restrict post edit/delete based on user ID and custom field
  32. get_post_meta returning empty string when data shows in the database
  33. publish_post action hook doesn’t give post_meta_data
  34. Remove WordPress.org Meta link
  35. Remove post meta keys
  36. How to access the post meta of a post that has just been published?
  37. Why time functions show invalid time zone when using ‘c’ time format?
  38. Why is get_post_meta returning an array when I specify it as single?
  39. How to update/delete array in post meta value?
  40. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  41. Adding an assisting editor box to Post page
  42. delete unused postmeta
  43. Should I sanitize custom post meta if it is going to be escaped later?
  44. Add post meta based on another post meta value before publish post
  45. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  46. Front-end update_post_meta snippet displays white screen?
  47. Query between two meta values?
  48. Save both current and new version of post meta
  49. Get Advanced Custom Fields values before saving [closed]
  50. Give extra post-meta to RSS feeds
  51. How to get meta value in wp_attachment_metadata
  52. WP REST API “rest_no_route” when trying to update meta
  53. Clean up output added via wp_head()
  54. List posts under meta_value heading
  55. Why am I getting an infinite loop with have_posts?
  56. get_post_meta – get a single value
  57. delete value 0 in post meta [closed]
  58. Can I safely delete a record, manually, in the wp postmeta table?
  59. How to store post meta in an array?
  60. What action hook updates post meta?
  61. Can’t translate the post meta data (Date) in another language
  62. get_post_meta / update_post_meta array
  63. adding a URL to a post meta
  64. Exclude a category from the filed under list
  65. Short of raw SQL, can I query for multiple attachment metadata that have a given array key?
  66. How do I access post meta data when publishing a new post in Gutenberg?
  67. update_post_meta() not working when used with WordPress action
  68. Using Advanced Custom Field (ACF) to insert meta description on each page
  69. Triple meta_key on custom SELECT query
  70. get_post_custom()
  71. Adding meta data to an attachment post
  72. update_post_meta not adding anything.(Nor add_post_meta)
  73. loop through all meta keys with get_post_meta
  74. Get posts by meta value with date
  75. How to add meta tag to wordpress posts filter?
  76. Are multiple values from get_post_meta guaranteed to be ordered?
  77. Identifying Importer Posts
  78. Get updated post meta on save_post action?
  79. Get post from meta_key and meta_value
  80. Add a post metadata if only the key and value does not exist
  81. get_post_meta returns bool(false)
  82. How metadata API works?
  83. Correct processing of `$_POST`, following WordPress Coding Standards
  84. Metabox Data not being saved [closed]
  85. How can I get values using key in Carbon Fields from Multiselect?
  86. Delete post meta conditionally after save post
  87. How to sanitize post meta field value?
  88. Problem With Order Item Meta In Woocommerce
  89. Job of meta_key meta_value fields in database tables
  90. WordPress Action Hooks and Post ID?
  91. Post IDs missing on delete_postmeta action hook
  92. How to store Gutenberg ColourPicker RGBA as metadata
  93. Options to get my custom post type metadata via the WordPress API
  94. Is it possible to update a post meta field through REST API if the format of it when registered is nested?
  95. How trigger to save post when updating post meta
  96. Create a Metabox that behaves Like a Taxonomy Box
  97. Views count with time limit per IP
  98. Generate an Email address from that of the Post Author
  99. Query 2 meta key values and a category
  100. trying to do if post meta !=0
Categories post-meta Tags post-meta
Upgrading from 3.5.2 to 3.9.1
Make a user administrator to a sub directory site and a contributor to main site in multisite network

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