Updating a related post’s “post_modified” value

Had a brainwave whilst typing up the question.. there are other actions that fire when a post is updated, such as post_updated which naturally update the post_modified timestamp.

Instead I decided to save the activity’s modified time as a meta value, and will sort on that instead and leave the post_modified functionality intact.

The trimmed down function:

function update_project_update_time($post_id) {
  // get the associated project
  $project = get_field('project', $post_id);
  if( $project ) {
    $time = get_the_date('Y-m-d H:i:s', $post_id);
    update_post_meta($project, 'last_modified', $time);
  }
}