Last active
          June 24, 2025 17:48 
        
      - 
      
- 
        Save joshuadavidnelson/d260b2e07bf22d434534f29927748b35 to your computer and use it in GitHub Desktop. 
    Stop WP All Import from changing the modified dates on import and, optionally, set the modified date.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /** | |
| * These functions hook into the WP All Import plugin to control the post_modified date values. | |
| * | |
| * Such that you can avoid updating the modified date when importing, as well as explicitly set it. | |
| * | |
| * @since 2025-06-19 | |
| **/ | |
| // Exit if accessed directly. | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| // Block modified date changes on imports. | |
| add_action( 'pmxi_before_post_import', 'keep_modified_date_on_import' ); | |
| /** | |
| * Adds a filter before the import to block updating the modified date. | |
| * | |
| * We hook into the core filter within the pmxi_before_post_import filter | |
| * so we are only adding this logic to imports. | |
| * | |
| * @param int $import_id | |
| * @return void | |
| */ | |
| function keep_modified_date_on_import( $import_id ) { | |
| add_filter( | |
| 'wp_insert_post_data', | |
| function ( $new, $old ) { | |
| $new['post_modified'] = $old['post_modified']; | |
| $new['post_modified_gmt'] = $old['post_modified_gmt']; | |
| return $new; | |
| }, | |
| 10, | |
| 2 | |
| ); | |
| } | |
| // Set modified dates from import data, if they exist. | |
| add_action( 'pmxi_saved_post', 'set_post_modified_dates', 10, 3 ); | |
| /** | |
| * This will update the modified date, if it's provided in the import. | |
| * | |
| * To set a modified date, you must have a `post_modified` column with the new date | |
| * in the correct format: `YYYY-MM-DD HH:MM:SS` | |
| * | |
| * @see https://www.wpallimport.com/documentation/action-reference/#pmxi_saved_post | |
| * | |
| * @param int $post_id The post id of the imported post. | |
| * @param SimpleXMLElement $xml_node The libxml resource of the current XML element. | |
| * @param bool $is_update True if this is an update, false if it's a new post. | |
| * @return void | |
| */ | |
| function set_post_modified_dates( $post_id, $xml_node, $is_update ) { | |
| // Only run on updates. | |
| if ( ! $is_update ) { | |
| return; | |
| } | |
| // Convert SimpleXml object to array for easier use. | |
| $record = json_decode( json_encode( ( array ) $xml_node ), 1 ); | |
| // Bail if we don't have what we need. | |
| if ( ! $post_id || ! $record || ! isset( $record['post_modified'] ) ) { | |
| return; | |
| } | |
| // Compare the modified date to the published date, | |
| // if the modified date is before the published date, we'll use the published date instead. | |
| $published_date = get_post_field( 'post_date', $post_id, 'raw' ); | |
| if ( strtotime( $record['post_modified'] ) < strtotime( $published_date ) ) { | |
| $record['post_modified'] = $published_date; | |
| } | |
| // Assuming your import file has <post_modified> and <post_modified_gmt> columns: | |
| $modified_date = mysql2date( 'Y-m-d H:i:s', sanitize_text_field( $record['post_modified'] ), false ); | |
| // Only run if we have a valid date. | |
| if ( $modified_date ) { | |
| wp_update_post( | |
| array( | |
| 'ID' => $post_id, | |
| 'post_modified' => $modified_date, | |
| 'post_modified_gmt' => get_gmt_from_date( $modified_date ), | |
| ), | |
| false, // don't return an error | |
| false // don't fire hooks on update | |
| ); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment