1

Would anyone know of a way to determine when a custom field for a post was last updated?

I'm trying to write something that says "If CUSTOMFIELD was updated in the last 24 hours don't do anything, else update it" but don't see any time stamps in the database for this.

Any clever ideas floating around?

  • Well, to update a custom field you would have to update the Post presumably ( by clicking the "Update" button ). So you could use modified_date or the_modified_date() function in the loop to get when it was updated. Unless of course you're updating postmeta via ajax or some other method. – Howdy_McGee Feb 3 '16 at 22:49
2

I would add another custom field that stores the update time.

Then create an add_action function for update_post_meta to also save the timestamp at the same time you write to CUSTOMFIELD.

The action triggered when adding a custom field is add_post_meta and when updating a custom field update_post_meta

  • This is the approach I ended up working with, thanks david_nash, along with Transients. – Nathan Feb 4 '16 at 17:19
2

I don't know what are you trying to achieve, but in my mind and opinion, you are definitely on the wrong track here.

When a custom field is updated, nothing happens to the actual post object as custom fields aren't part of the WP_Post object. This means that no actions are triggered related to updating the post object itself, and therefor the post_modified and post_modified_gmt dates remain the same. This all comes down to that you cannot use the post object as a guideline as to when a custom field was updated. For the sake of interest, be sure to read this answer by @gmazzap about which hooks firing when a post is updated

Custom fields by itself also does not save any info about creation, deletetion or modified dates and times. For this you will need to write your own API and somehow store these relevant dates in db, and then call upon these saved dates for checking purposes before what you are suppose to run on success and failure.

You will most probably need to look at hooks like updated_post_meta which fires after a custom field is successfully updated and then act on that to update some value in some field which is created by your own API. You can also check out this answer by @JanFabry about all relevant functions and actions regarding custom fields

TO CONCLUDE

I wonder if it is even worth the trouble writing an extensive API for this. You will first need to go and rethink your complete approach to this, and if you really need to do this with custom fields, you will need to write an API which will handle the complete CRUD process reliably and safely

EDIT

To avoid confusion and probably wrong interpretation, we are talking about updating a custom field, which in the sense of this post means that we only click the Update button in the Custom Fields meta box. By only clicking that Update button, nothing happens to the post and the modified dates stay the same.

The modified dates only changes when we explicitly click on the Update button in the Publish meta box. Although the post object doesn't change, the post modified dates are updated as if the post object has changed. Just remember, we do not need to click this Update button to update a custom field, which makes any method relying on this as a custom field updated very unreliable

  • I actually just tested this and it looks like no matter what, the modified date is always updated whenever the admin clicks the "Update" button. Now, a revision post isn't created because nothing has change. But it's still an unreliable method of checking if postmeta has been updated as the modified date could mean anything changed or nothing changed. It just means that somebody has clicked "Update" in the admin panel. – Howdy_McGee Feb 4 '16 at 15:06
  • 1
    @Howdy_McGee Yes, remember, we are talking about updating a custom field, not the post, so we click on the update button in the custom field section. The modified dates does only change when we click the update button in the publish meta box, not the update buttons in the custom fields meta box ;-) – Pieter Goosen Feb 4 '16 at 15:38
  • Thanks for the detailed response and links. As to what I am trying to achieve, it was specifically to only update a custom field's value if it hadn't been updated within the past few hours or so. It was for a high traffic site and for my purposes, updating the meta data multiple times per second didn't seem like a great plan resource-wise. – Nathan Feb 4 '16 at 17:22
  • My pleasure. Glad you got your issue solved in the end. Enjoy and good luck on your project ;-) – Pieter Goosen Feb 4 '16 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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