• Resolved alexnn

    (@alexnn)


    Hi there,
    I need to prohibit importing all rows with irrelevant dates in the {date} field on rule “{date}”>=Today. Or how to exclude the import of CSV lines that do not meet this rule?
    How it is better to make xPath on the first step or PHP function?
    Can you show an example of this php code how to do it?
    Please help me)

    Thanks!

    • This topic was modified 6 years, 8 months ago by alexnn.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @alexnn

    Unfortunately, it is not possible to filter by date in the filtering options section of the import. Instead, you’ll need to use the “wp_all_import_is_post_to_create” hook: https://github.com/soflyy/wp-all-import-action-reference/blob/master/all-import/wp_all_import_is_post_to_create.php.

    Here’s an example snippet that you can adjust as needed:

    function my_is_post_to_create( $continue_import, $data, $import_id ) {
        if ( $import_id == 18 ) { // Change this to your actual import ID.
    		if ( strtotime( $data['date'] ) >= strtotime( "Today" ) ) {
    			return false;
    		} else {
    			return true;
    		}
    	}
        return true;
    }
    add_filter('wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3);
    Thread Starter alexnn

    (@alexnn)

    Hi!
    Thanks for such support!
    Firstly I try to prepare a date from the string.
    I try this code

    $desc='{string[1]}';
    $data = preg_replace("/(.*?)((\d{1,2})-(\d{1,2})-(\d{4}))/i", "$5-$4-$3", $desc);
    $data = strtotime( $data );
    function my_is_post_to_create( $continue_import, $data, $import_id ) {
        if ( $import_id == 8 ) { // Change this to your actual import ID.
    		if ( $data >= strtotime( "Today" ) ) {
    			return false;
    		} else {
    			return true;
    		}
    	}
        return true;
    }
    
    add_filter('wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3);

    but all the records are skipped…SKIPPED: By filter wp_all_import_is_post_to_create….
    What could be wrong?

    Plugin Author WP All Import

    (@wpallimport)

    Hi @alexnn

    You’ll need to grab the date from within the $data array in the my_is_post_to_create function. Assuming that the date is in the import element {string[1]}, something like this should work:

    function my_is_post_to_create( $continue_import, $data, $import_id ) {
        $date = preg_replace("/(.*?)((\d{1,2})-(\d{1,2})-(\d{4}))/i", "$5-$4-$3", $data['string']);
        $date = strtotime( $date );
        if ( $import_id == 8 ) { // Change this to your actual import ID.
    		if ( $date >= strtotime( "Today" ) ) {
    			return false;
    		} else {
    			return true;
    		}
    	}
        return true;
    }
    
    add_filter('wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3);
    Thread Starter alexnn

    (@alexnn)

    Hi!
    Unfortunately this code does not work.
    My {string[1]} contain “words blabla and date(d-m-Y format)”.
    What date format does the function $data understand?
    $data[‘format???’]

    Plugin Author WP All Import

    (@wpallimport)

    Hi @alexnn

    The $data variable is an array of your import elements and their values, it does not recognize or convert date formats. You must extract the date from the $data array and then parse it.

    Keep in mind that the code we provided is only example code, you’ll need to adjust it to work with your data. We, unfortunately, can’t write custom code for your project.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PHP function or xPath?’ is closed to new replies.