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 to add a new meta key and assign timestamp to posts

A very simplistic method of achieving this would be as follows;

add_action('template_redirect', 'recently_read');
function recently_read(){

    global $post;

    //only run this function for the posts post_type
    if ( !is_single() ) 
        return;

    //get the current time in the format of: 2013-02-22 11:55:51 
    $timestamp = current_time('mysql'); 

    //add the $timestamp variable to the meta_key for this post
    update_post_meta($post->ID, 'reading_this', $timestamp);


}

Note: The above function goes into your theme functions.php file and is hooked onto the template_redirect action which is fired just before displaying the page to the user. At this point, behind the scenes, our recently_read function will fire and add a time stamp under the key reading_this (name it to your liking). This will occur each time this or any post is viewed. If a time stamp already exists for a given post, it will then update the time stamp accordingly.

Then to retrieve a list of posts with this key you would do the following in your theme files where you want those posts to display;

$recently_read = get_posts( 
    array(
        'posts_per_page'  => 5,       //how many posts we want to show at most
        'meta_key' => 'reading_this', //get posts by our meta key
        'orderby' => 'meta_value',    //order posts by the value stored in the key
        'order' => 'DESC'             //order posts in descending order (newest to oldest)
   )
); 

foreach ($recently_read as $read) {
    echo '<a href="'. get_permalink($read->ID) . '">'. $read->post_title .'</a>';
}

The above could be improved a number of ways but this will definitely get you started.

Note: This is void of any logic that determines if the post was viewed in the last X minutes. For that you can apply your own logic to the meta_key named reading_this and manipulate the data to your liking.

Related Posts:

  1. Ordering posts by anniversary using only day and month
  2. Displaying posts with only upcoming dates according their custom field date value
  3. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  4. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  5. Ordering posts by custom field named “date” in backend
  6. Unique meta_key with array value vs repeated meta_key with single values
  7. Avoiding ACF get_field and returning to core WordPress function
  8. change order of images attached to post
  9. How to improve my non-unique metadata MySQL entries?
  10. WP Query Args – search by meta_key or title
  11. Can I exclude a post by meta key using pre_get_posts function?
  12. What is the index [0] for on post meta fields?
  13. Matching Serialized Arrays with meta_query
  14. Custom field metabox not showing in back-end
  15. So much data in postmeta
  16. How to hide meta box values from custom fields list?
  17. get_post_meta() unserialize issue – returns boolean(false)
  18. What is the advantage of the wp_options design pattern?
  19. How Can I save multiple records in same meta key?
  20. Meta keywords and descriptions plugin for manually editing meta for each page/post
  21. Trying to perform complex custom field query with order by set to field value
  22. Up/Down voting system for WordPress
  23. post meta data clearing on autosave
  24. WordPress Search Custom Meta Field Only
  25. Display info from custom fields in all images’ HTML
  26. Loop through two different sets of custom fields
  27. Update meta values with AJAX
  28. copy attachments to another post type and change attachment url
  29. Custom fields to save multiple values
  30. Difference between ‘LIKE’ and ‘IN’ in meta queries
  31. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  32. Saving custom image meta fields
  33. Get all meta keys assigned to a post type
  34. Complex WP_User_Query call fails on production server
  35. Adding custom fields (post meta) before/during wp_insert_post()
  36. How to query posts with certain custom meta data, and output Post data
  37. post meta getting deleted on save
  38. Custom field not updating when value is empty
  39. meta_compare seems to be treating values as strings instead of integers as expected
  40. Limit the number of acf content when displaying in post loop [closed]
  41. Read / Watch / Listen times – meta
  42. How to add custom metadata text box dropdown to sidebar in Gutenberg editor for all post types
  43. Why is my Custom Meta Box Field Inputs NOT saving?
  44. Get registered custom fields or post meta even if empty
  45. Alter main archive, to show posts with meta as last
  46. Displaying multiple URLs as custom field values
  47. Show values of custom post meta on ‘Add new post’ page?
  48. Custom post meta field effect on the performance on the post
  49. get_posts in meta box dropdown not showing latest posts
  50. Use meta_query to get title of associated post
  51. Limits, not all post are showen when querying for posts by view count
  52. Display Custom Meta Box Field Only If Value is Present
  53. WP Query – Is this correct?
  54. comment meta_query for keys that aren’t yet set
  55. Branch made by several custom values
  56. Looping inside block return
  57. how can I show name and value of Custom Fields together?
  58. tax query between operator like
  59. Gravity Forms Update post meta from template [closed]
  60. Best way to achieve multiple links in a post title
  61. Is it possible to compare the current time with a custom “start” and “end date
  62. How can I modify my meta_query to work with prices that are stored in the database that contain dollar signs and commas?
  63. custom filed from post in the side bar
  64. wordpress multi user question
  65. multiple meta_query and orderby question
  66. Adding Custom Metadata to my Archive/Posts page
  67. Trouble with serialized metadata
  68. Query against multiple locations within single custom post type post
  69. Custom Fields Not Showing (ACF not installed)
  70. SQL query to change the value of a Custom Field
  71. Custom meta POST request fired twice when updating a post in Gutenberg
  72. Add custom field information to source meta data
  73. How do I update custom field post meta in frontend post template?
  74. Query multiple meta values at the same time :
  75. How to Find List of Available Custom Fields for Theme?
  76. Create Shortcode shows only posts with custom_field meta
  77. Custom Fields Not Working In Footer
  78. ACF plugin and field update
  79. Delete custom meta
  80. Search one custom field?
  81. Can’t save meta field value if the title not set
  82. Adding custom fields to images
  83. How to save dynamically generated value in post type?
  84. wordpress simple post multi rating with post_meta and user_meta
  85. Query Posts Via WordPress URL
  86. how to show this part only in the single post page?
  87. Display meta data from a custom field within plugin Category Grid View Gallery
  88. Checking if field is set before comparing with meta_query in query_posts?
  89. Querying meta values within an array
  90. Custom fields / meta box output
  91. If post custom meta data is not set, show normal state
  92. Colecting values from custom field checkboxes and displaying them in the post
  93. Build Array from Input Fields question
  94. How do delete a meta key?
  95. custom field with links
  96. How can i put a custom field inside this php
  97. Check for custom field value in different post type than current one and do something
  98. How to add new Metadata options (Date, Author, etc.) for Posts?
  99. Saving multiple custom meta box fields
  100. Search for meta_query does not return any result if combined with title
Categories custom-field Tags custom-field, meta-query, post-meta, timestamp
Using wp_list_categories like the the_category (showing just current categories of a post)
How can I automatically insert the page content as the title?

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