96

How to Order Imported Items in WP All Import by Customizing Post Dates

Powered by WPCodeBox

Optimize your WP All Import with custom post dates to accurately order imported items from your CSV/XML file, ensuring they display exactly as imported.


<?php
if ( ! function_exists( 'wpcb_set_imported_item_date' ) && ! function_exists( 'wpcb_reset_imported_item_date' ) ) {
    function wpcb_set_imported_item_date( $id ) {
    	if ( $post = get_post( $id ) ) {
    		$date = get_option( 'my_custom_date_option', date( "Y-m-d H:i:s", time() ) );
    		
    		if ( $date_obj = new DateTime( $date ) ) {
    			$date_obj->modify( '-1 second' );
    			$new_date = $date_obj->format( 'Y-m-d H:i:s' );
    			update_option( 'my_custom_date_option', $new_date );
    			wp_update_post( array(
    				'ID' 			=> $id,
    				'post_date' 	=> $new_date,
    				'post_date_gmt' => get_gmt_from_date( $new_date )
    			) );
    		}
    	}
    }
    
    function wpcb_reset_imported_item_date( $import_id ) {
    	delete_option( 'my_custom_date_option' );
    }
}

add_action( 'pmxi_saved_post', 'wpcb_set_imported_item_date', 10, 1 );
add_action( 'pmxi_after_xml_import', 'wpcb_reset_imported_item_date', 10, 1 );

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.