WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi there we are using this ajax function to update the views of our custom "posts"

    if( !empty( $_REQUEST['action']) && $_REQUEST['action'] == 'update_view_count' && !empty($_REQUEST['attach_id']) ) {
    $count = get_post_meta($_REQUEST['attach_id'], 'view_count', true);
    if( !is_numeric($count) ) $count = 0;
    if( update_post_meta($_REQUEST['attach_id'], 'view_count', $count + 1) === FALSE ) {
    echo json_encode(array('error' => 'Failed to update view_count for ' . $_REQUEST['attach_id']));
    else {
    echo json_encode(array('message' => 'Updated ' . $_REQUEST['attach_id'] . ' view_count to ' . ($count + 1)));

We want to add time stamp on it. Any ideas how to do that? I also wanted to add that we don't need to keep track indefinitely, we need it to be tracked for 5 days, then reset.

share|improve this question

You are nearly there, I'd say. I am sure there are many ways to do this. If it were me, I'd build a 'nested' array for this. Something like...

    'timestamp-1' = array(
        'message' => 'Updated post1',
        'message' => 'Updated post1'
    'timestamp-2' = array(
        'message' => 'Updated post3',
        'message' => 'Updated post4',
        'message' => 'Updated post5'

... where 'timestamp-N' is midnight or midday or some arbitrary value for a particular day. You can run wp-cron daily to check the array and clean it up. I suggest doing it this way because using a minute-and-second timestamp means having to check and manipulate that array pretty much every page load.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.