Docs>Importer>Code Snippets

How to filter which xml and csv records are imported

New to version 2.1.0 of ImportWP you can restrict wich rows will be imported using the new ‘iwp/importer/skip_record’ wordpress filter, returns true to skip, or false to continue with the import.

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();

Next Article