How to filter records before importing
Table of Contents
Filter records via ImportWP interface
Import WP allows you to create filters on your xml or csv file before each record is imported, filters can be added via the Filter Rows section found under the Run Import tab of the edit importer screen, or if you prefer more complex filters can be added in code.
Clicking add filter button will expand the filter ui allowing you to add multiple conditions by clicking the and or or buttons.
Filter conditions
To best explain each condition, the following dataset will be used for the following examples.
1, saw
2, circular saw
3, circular saw blade
4, red circular saw
5, blue saw
6, drill
7, cordless drill
8, cordless drill bit
9, red cordless drill
10, blue drill
Available conditions
Text equals
The Text Equals condition allows you to skip row/records that match the input, using the previously defined dataset and skipping rows where the text equals circular saw will skip the 2nd record, and import the remaining records.
Records Imported where skipped record text equals ‘circular saw’:
1, saw
3, circular saw blade
4, red circular saw
5, blue saw
6, drill
7, cordless drill
8, cordless drill bit
9, red cordless drill
10, blue drill
Text not equals
The Text not equals condition allows you to skip row/records that do not match the input, using the previously defined dataset and skipping rows where the text does not equal circular saw will skip all records except for the 2nd record.
Records Imported where skipped record text not equals ‘circular saw’:
2, circular saw
Text contains
The Text Contains condition allows you to skip row/records that contain part of the input, using the previously defined dataset and skipping rows where the text equals circular saw will skip the 2nd – 4th records, and import the remaining.
Records Imported where skipped record text contains ‘circular saw’:
1, saw
5, blue saw
6, drill
7, cordless drill
8, cordless drill bit
9, red cordless drill
10, blue drill
Text not contains
The Text Not Contains condition allows you to skip row/records that do not contain part of the input, using the previously defined dataset and skipping rows where circular saw is not found, skipping the 1st, 5-10th.
Records Imported where skipped record text does not contain ‘circular saw’:
2, circular saw
3, circular saw blade
4, red circular saw
Text is listed in
The Text is listed in condition allows you to skip row/records where they equal an item in the list, using the previouslt defined dataset and skipping rows where the value is either circular saw or cordless drill will skip the 2nd and 7th rows.
Records Imported where skipped record text is listed in either ‘circular saw’ or ‘cordless drill’:
1, saw
3, circular saw blade
4, red circular saw
5, blue saw
6, drill
8, cordless drill bit
9, red cordless drill
10, blue drill
Text not listed in
The Text not listed in condtion allows you to skip row/records where they do not equal an item in the list, using the previously defined dataset and skipping rows where the values are not circular saw and cordless drill will skip the 1st, 3-6th and 8-10th rows.
Records Imported where skipped record text is not listed in either ‘circular saw’ or ‘cordless drill’:
2, circular saw
7, cordless drill
Contains text in list
The Contains text in list condition allows you to skip row/records where they are not found in the list, using the previously defined dataset and skipping rows where the values match circular saw or cordless drill will skip 2-4th, 7-9th rows.
Records Imported where skipped record text contains text in list ‘circular saw’ or ‘cordless drill’:
1, saw
5, blue saw
6, drill
10, blue drill
Does not contain text in list
The Does not contain text in list condition allows you to skip row/records where they are found in the list, using the previously defined dataset and skipping rows where the values do not match circular saw or cordless drill will skip the 1st, 5-6th, 10th rows.
Records Imported where skipped record text does not contains text in list ‘circular saw’ or ‘cordless drill’:
2, circular saw
3, circular saw blade
4, red circular saw
7, cordless drill
8, cordless drill bit
9, red cordless drill
Filter rows using custom code
Filter rows using template data
This example will check to see if the post_status data from the importer template field has the status of draft, returning true if matched. Using the ParsedData argument we can get the value of a template field.
/**
* Filter Records
*
* @param bool $result
* @param ParsedData $data
* @return void
*/
add_filter('iwp/importer/skip_record', function($result, $data){
// skip the record if the post_status data matches 'draft'
$post_status = $data->getValue('post.post_status');
if($post_status === 'draft'){
return true;
}
return $result;
}, 10, 2);
Filter rows using the CSV Parser
This example will check the first column of the csv file for the text ‘SKIP’, returning true if matched. Using the third argument to access the CSV Parser via the importer allowing you to run custom queries on the csv data.
/**
* Filter CSV Records
*
* @param bool $result
* @param ParsedData $data
* @param Importer $importer
* @return void
*/
add_filter('iwp/importer/skip_record', function($result, $data, $importer){
/**
* @var CSVParser $parser
*/
$parser = $importer->getParser();
// skip the csv row if the first column in the csv matches 'SKIP'
if ($parser->query('0') == 'SKIP') {
return true;
}
return $result;
}, 10, 3);
Filter records using the XML Parser
This example will check the xml record for the status element, returning true if that element contains the text ‘draft’, Using the third argument to access the XML Parser via the importer allowing you to run custom queries on the xml data.
/**
* Filter XML Records
*
* @param bool $result
* @param ParsedData $data
* @param Importer $importer
* @return void
*/
add_filter('iwp/importer/skip_record', function($result, $data, $importer){
/**
* @var XMLParser $parser
*/
$parser = $importer->getParser();
// skip the xml record if the element in the xml matches 'draft'
if($parser->query('/status') === 'draft'){
return true;
}
return $result;
}, 10, 3);
Filtering rows on a specific importer
This example will show you how to apply the new ‘iwp/importer/skip_record’ filter to run on a specific importer using the importers id, this can easily be modified to get the template or any other importer data from the importer model instead of just the id.
<?php
use ImportWP\Common\Importer\Importer;
use ImportWP\Common\Importer\ParsedData;
use ImportWP\Common\Model\ImporterModel;
use ImportWP\EventHandler;
class IWP_Example_CSV_Filter
{
private $id = null;
public function __construct()
{
add_action('iwp/register_events', [$this, 'register_events']);
}
/**
* Register Events
*
* @param EventHandler $event_handler
* @return void
*/
public function register_events($event_handler)
{
$event_handler->listen('importer_manager.import', [$this, 'before_import']);
$event_handler->listen('importer_manager.import_shutdown', [$this, 'after_import']);
}
/**
* Before importer init
*
* @param ImporterModel $importer_model
* @return void
*/
public function before_import($importer_model)
{
// Capture the importer id to restrict what importers are filtered
$this->id = $importer_model->getId();
add_filter('iwp/importer/skip_record', [$this, 'filter_records'], 10, 3);
}
/**
* Filter Records
*
* @param bool $result
* @param ParsedData $data
* @param Importer $importer
* @return void
*/
public function filter_records($result, $data, $importer)
{
// TODO: Add your custom filter conditions here
return $result;
}
public function after_import()
{
$this->id = null;
remove_filter('iwp/importer/skip_record', [$this, 'filter_records'], 10, 3);
}
}
new IWP_Example_CSV_Filter();