Skip to content

Instantly share code, notes, and snippets.

@arraypress
Last active June 13, 2022 10:27
Show Gist options
  • Save arraypress/a13af4ffc84b23766f2aa14ef10f31c3 to your computer and use it in GitHub Desktop.
Save arraypress/a13af4ffc84b23766f2aa14ef10f31c3 to your computer and use it in GitHub Desktop.
TImestamp comparison
/**
* Compare the import date (timestamp) with an existing post meta date.
*
* @param $continue_import bool
* @param $post_id int
* @param $data array
* @param $import_id int
*
* @return bool|mixed
*/
function dm_filter_by_date( $continue_import, $post_id, $data, $import_id ) {
$import_id = absint( $import_id );
if ( ! in_array( $import_id, array( 121, 124 ) ) ) {
return $continue_import;
}
// Bail if no post ID.
if ( empty( $post_id ) ) {
return $continue_import;
}
// Retrieve meta which appears to already be stored as a timestamp.
$existing_timestamp = get_post_meta( $post_id, 'wpcf-letzteaenderung', true );
// Skip if existing date does not
if ( empty( $existing_timestamp ) ) {
return $continue_import;
}
// Change this to whatever field you want to compare against.
$import_field = 'letzteaenderung';
// Verify we have an actual valid data and field.
if ( ! empty( $data ) && isset( $data[ $import_field ] ) ) {
// Change 'import' field to a Unix timestamp.
$import_timestamp = strtotime( $data[ $import_field ] );
// Compare file date with existing date.
if ( $import_timestamp > $existing_timestamp ) {
// Create or update record if file date >= target date.
return true;
} else {
// Do not create or update the record otherwise.
return false;
}
}
// Do nothing if it's not our target import.
return $continue_import;
}
add_filter( 'wp_all_import_is_post_to_update', 'dm_filter_by_date', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment