Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For some reasons I show the Last Modified information to my users in the blog. Recently I have to clear up my categories and tags, but these actions will result in the update of 'Last Modified' field of a post, which is not actually I desired.

Content updates are what readers really concerned about, and not about meta information (like tags, categories, custom fields, descriptions, etc).

So is there a way to update the 'Last Modified' field of a post only when the content is changed?

share|improve this question

2 Answers 2

I guess, there is no exact way of retrieving the time only when the post's content is updated until you change the core functionality of wordpress and that might lead to some other issues. But here is a workaround I would like to propose here.

You can retrieve the time/date only when content is updated via revisions. A revision is created only when you change the content of the post, not otherwise.

But there is no exact way of retrieving only date/time but a set of parameters.

Use this code and it'll give you a string that contains the required data:

<?php 
    echo wp_post_revision_title_expanded($post,false);
?>

What you can do now is fetch the complete string in a variable and use regular expression to extract the desired stuff.

Reference: Codex link. Nut nothing much is available here.

EDIT: (thanx for the function update)

This is the code based on wp_post_revision_title() and working as expected.

<?php
    $revision_data = wp_post_revision_title( $post ,false );
    $date_time = explode('@', explode('[', $revision_data)[0]);
    echo '<p>Date:'.$date_time[0].'</p>';
    echo '<p>Time:'.$date_time[1].'</p>';
?>
share|improve this answer
    
Thanks for sharing! After looking around post-template.php which is the location of function wp_post_revision_title_expended, I found another function named wp_post_revisions_title which will have a better output. I'll study into this. –  renfei Feb 20 at 8:14
    
Upon investigating further, I have noticed that the wp_post_revision_title function will oddly return time that subject to every change of the article, which is equivalent to calling the_modified_time. However wp_get_post_revisions function will return an array of revisions and autosaves that only update when author, content, excerpt or title change. So we can do it by implementing a custom function which iterates the return value of the above-mentioned function and find first non-autosave record (wp_is_post_autosave helps here), then extract the post_modified field as we need. –  renfei Feb 20 at 12:04

Crude and minimally tested but...

function post_content_check_wpse_135268($data,$postarr){
  if (empty($postarr['ID'])) return $data;
  $old = get_post($postarr['ID']);
  if (!empty($old) && ($old->post_content === $data['post_content'])) {
    $data['post_modified'] = $old->post_modified;
    $data['post_modified_gmt'] = $old->post_modified_gmt;
  }
  return $data;
}
add_action('wp_insert_post_data','post_content_check_wpse_135268',10,2);

The code compares the existing post content in the database to the new post content. If they are the same the old modified date information is used. That should mean that the date gets changed only if the post content is changed.

The comparison is very crude-- a simple ===. You probably want something more complex-- for example, something that will ignore spurious whitespace.

share|improve this answer

Your Answer

 
discard

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.