| 5485 | * Internal function that collects all updated post IDs when meta was changed. |
| 5486 | * |
| 5487 | * Modified post date will be updated later on the shutdown hook. |
| 5488 | * |
| 5489 | * @since 4.1 |
| 5490 | * |
| 5491 | * @param int $post_id the ID of the modified Post object. |
| 5492 | */ |
| 5493 | function _collect_updated_postmeta_post_ids( $post_id ) { |
| 5494 | global $updated_post_ids; |
| 5495 | |
| 5496 | if ( empty( $updated_post_ids ) ) { |
| 5497 | $updated_post_ids = array(); |
| 5498 | } |
| 5499 | if ( ! in_array( $post_id, $updated_post_ids ) ) { |
| 5500 | $updated_post_ids[] = $post_id; |
| 5501 | } |
| 5502 | } |
| 5503 | |
| 5504 | /** |
| 5505 | * Helper function for updating the post modified date. |
| 5506 | * |
| 5507 | * Iterates over all post IDs updated during the request when meta has been changed. |
| 5508 | * |
| 5509 | * @since 4.1 |
| 5510 | * |
| 5511 | */ |
| 5512 | function update_post_modified_dates() { |
| 5513 | global $updated_post_ids; |
| 5514 | |
| 5515 | // Check whether posts' metadata has been changed during the request |
| 5516 | if ( ! empty( $updated_post_ids ) && is_array( $updated_post_ids ) ) { |
| 5517 | $post_modified = current_time( 'mysql' ); |
| 5518 | $post_modified_gmt = current_time( 'mysql', 1 ); |
| 5519 | global $wpdb; |
| 5520 | |
| 5521 | $updated_fields = array( |
| 5522 | 'post_modified' => $post_modified, |
| 5523 | 'post_modified_gmt' => $post_modified_gmt |
| 5524 | ); |
| 5525 | |
| 5526 | // Update the post_modified dates for each modified post |
| 5527 | foreach ( $updated_post_ids as $post_id ) { |
| 5528 | $where = array( 'ID' => $post_id ); |
| 5529 | $wpdb->update( $wpdb->posts, $updated_fields, $where ); |
| 5530 | |
| 5531 | clean_post_cache( $post_id ); |
| 5532 | } |
| 5533 | } |
| 5534 | } |
| 5535 | add_action( 'shutdown', 'update_post_modified_dates' ); |
| 5536 | |
| 5537 | /** |