What exactly does the import tool do?

I experimented with the import tool a bit and it seems to do different things for different tables.

For posts: if a post (or page, which technically is a post too) title from the import file is already present in the database, it won’t be imported. The WordPress interface in this case will indicate that “there already exists…”.

Read More

For postmeta: all rows from the imported database will be added. Regardless whether that information was already present -> tends to bloat the database.

I wonder what happens for the other tables. Will rows be overwritten, added (as in postmeta) or refused (as in posts) if they’re already present?

And how is “already present” defined here anyway (post_id? term_id? name)?

Related posts

1 comment

  1. how is “already present” defined here anyway (post_id? term_id? name)?

    The post title and date must match to be already present.

    The WordPress post_exists() function is used to determine if the post exists.

    From the plugin code:

    $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
    

    The is no filter provided to change this behavior.


    For posts: if a post (or page, which technically is a post too) title from the import file is already present in the database, it won’t be imported. The WordPress interface in this case will indicate that “there already exists…”.

    More than just 'post' and 'page' post types can be imported. Any defined post type can be imported. For example, the 'nav_menu_item' is handled differently in the import code:

    if ( 'nav_menu_item' == $post['post_type'] ) {
        $this->process_menu_item( $post );
        [...]
    

    The import doesn’t create a new post if:

    1. The post type doesn’t exist,
    2. The given post ID is already noted as imported or
    3. A post with the same title and date already exists.

    Posts marked as having a parent which do not exist in the database will become top level items. This may be temporary as the parent item may not have been imported yet.


    For postmeta: all rows from the imported database will be added. Regardless whether that information was already present -> tends to bloat the database.

    For posts that already exist, new / updated post terms, comments and meta are imported only if a post with the same title and date already exists (#3 above).

    The plugin code to add post meta follows:

    if ( ! isset( $post['postmeta'] ) )
        $post['postmeta'] = array();
    
    $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
    
    // add/update post meta
    if ( ! empty( $post['postmeta'] ) ) {
        foreach ( $post['postmeta'] as $meta ) {
            $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
            $value = false;
    
            if ( '_edit_last' == $key ) {
                if ( isset( $this->processed_authors[intval($meta['value'])] ) )
                    $value = $this->processed_authors[intval($meta['value'])];
                else
                    $key = false;
            }
    
            if ( $key ) {
                // export gets meta straight from the DB so could have a serialized string
                if ( ! $value )
                    $value = maybe_unserialize( $meta['value'] );
    
                add_post_meta( $post_id, $key, $value );
                do_action( 'import_post_meta', $post_id, $key, $value );
    
                // if the post has a featured image, take note of this in case of remap
                if ( '_thumbnail_id' == $key )
                    $this->featured_images[$post_id] = (int) $value;
            }
        }
    }
    

    Use the 'wp_import_post_meta' filter to adjust post meta prior to insert. The insert can be cancelled by returning an empty value, like 0.


    I wonder what happens for the other tables.

    All the information above was gleaned by reading plugin code (wordpress-importerwordpress-importer.php. Much of it is written in plain English in the inline documentation.

    There are no SQL queries run in this code. All information added to the database is done by using WordPress functions. To find out exactly how any table is affected, read the code.

Comments are closed.