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 you update post date (year only) in a separate custom field?

So I was first looking for a way to loop through the existing posts, as suggested in Christine’s comment. Based on this answer, I created a basic plugin that runs the loop upon activation.

// Run the loop when the plugin is activated
register_activation_hook(__FILE__, 'update_my_metadata');
function update_my_metadata(){
$args = array(
    'post_type' => 'post', // Only get the posts
    'post_status' => 'publish', // Only the posts that are published
    'posts_per_page'   => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
    // Run a loop and update every meta data
    $month=get_the_date('M');
    $year=get_the_date('Y');
    update_post_meta($post->ID, 'month-field', $month); echo $month;
    update_post_meta($post->ID, 'year-field', $year); echo $year;
 }
}

However, when I inspect post meta fields, both the month and year are only storing empty strings ” instead of the actual date value.

Any suggestions?

Related Posts:

  1. Update all posts automatically when using post_meta
  2. Better post meta efficiency?
  3. Human Time Diff, change mins to minutes
  4. Change post format using custom field
  5. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  6. What Is meta_id In wp_postmeta?
  7. Add custom field automatically (add_post_meta) with value based on number of words of article
  8. How can I show many posts an author has per week?
  9. If modified on same day, show only time
  10. How can I sort posts by the date and a custom meta field?
  11. Cannot retrieve a custom RSS field from posts
  12. Customize rel=canonical tag for single blog post
  13. How to I retrieve the ID from the Posts page?
  14. Changing default WP-Site creation date
  15. if in category but only with post meta
  16. Saving Post Data in Another Database
  17. Add multiple meta keys to a post at once
  18. Date is wrong on ‘all posts’ page
  19. saving/reading custom field value does not work – no value gets POSTed
  20. Post meta not working
  21. Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
  22. Display Updated Date Instead of Published Date
  23. How to hide meta from search result only on pages?
  24. How do I display posts ordered by a date custom field?
  25. WP_Query sort by meta_value_num or date
  26. Creating Ordered Query using Meta_key
  27. How can I set and update the the_date according to a custom field of the post
  28. How enter custom post meta or custom field on box post list archive page?
  29. Hide custom metabox value from custom fields
  30. Get post meta retrieving wrong value
  31. How to get posts published on the latest date?
  32. wpColorPicker – problem with implementation to post meta
  33. Edit multiple custom post types while saving a new or edited post
  34. Change post author without using wp_update_post()
  35. What date to use as a post date? date_gmt or modified_date_gmt
  36. wp_query with ajax
  37. Update post meta within save_post action
  38. How to update all posts at once?
  39. tribe_get_start_time displays the current date and time on other post types than tribe_events
  40. Display post number by category
  41. Output Post with ACF Fields into other Post
  42. Updating post meta for checkbox
  43. Get the most recently modified post date of most recently modified post
  44. Assigning Two Different Post Dates For Single Post
  45. Using system date format
  46. Add two custom fields as a default to the “new post” page.
  47. Custom permalink for each post
  48. Modify this line to give alternate format date?
  49. How can I tell if a post has been published at least once?
  50. How to show different timestamp
  51. Post: how to set created date after post has been published [closed]
  52. Is there a way getting post fields using `transition_post_status`
  53. Automatic blog page with custom design [closed]
  54. After ajax image is outside the “ tag
  55. Edit custom fields in posts list
  56. Add “Posted on” to post date
  57. Auto update post title and slug when post status is changed
  58. Show number of posts by logged in user
  59. How does WP decide how to display a page
  60. WP_Query custom field pass the post id
  61. wp_query if have posts show date but not in loop
  62. Retrieving posts by their date and category
  63. When sending a newsletter -not with wordpress- the server has 100% cpu [closed]
  64. Retrieve data from post meta meta_key having ‘_product_fq_image’ while having author_id and post type is PRODUCT
  65. Insert data from custom created PHP page into wp_postmeta table
  66. Adding a meta box to determine the sidebar [closed]
  67. The loop starting at a certain ID
  68. change the post time on multiple posts
  69. If custom field is empty, use one from a previous post
  70. Getting an extra post’s meta data?
  71. Simple Custom Metabox Not Saving
  72. Access last visit time to a post
  73. Get post category as a separate string and url
  74. Plotting posts on a graph
  75. Why my meta boxes won’t show in front post page?
  76. Adding external field to my post form (admin side) and to post show (frontend side)
  77. Randomizing Post Links Outside of Loop – No Author or Date
  78. Order posts by date
  79. How To Use Custom Fields With .mp3 Links
  80. Media attachment date and post date
  81. how can display a post on home screen without images
  82. Display content between two dates?
  83. Group Posts By Custom Meta Value: Date
  84. Will multiple category descriptions effect my search engine optimization?
  85. Automate post update for all posts?
  86. Post navigation using date
  87. Get post publishing date from within custom HTML block
  88. Post date automatically +100 years into the future
  89. What snippet do I need to type to show my ACF field show up on my theme?
  90. How to save template data into wp_post table (post_content column)
  91. Bulk update published posts date randomly using wp-cli?
  92. Is there a way to make [Table Of Content] plugin while not using revision data?
  93. Override WordPress core post-template.php block template
  94. Only the most recent post is showing on my category page (working on localhost, not live site)
  95. Post picker custom field on page
  96. Featured Images most often doesnt appear
  97. If Post Published Date or Modified Date is 1 Year or Older, Display Notice on Post Page
  98. Custom Fields – How to get the list of a specific active widget each time it rendered
  99. How to bulk Update URLs to new values?
  100. Select another post in a post meta like a parent page is selected while editing a page
Categories posts Tags custom-field, date, date-time, post-meta, posts
Enable plugins for a specific user role
How to define a post category undeletable by other users?

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