WP_Import::backfill_parents PHP Method

backfill_parents() public method

An imported post's parent may not have been imported when it was first created so try again. Similarly for child menu items and menu items which were missing the object (e.g. post) they represent in the menu
public backfill_parents ( )
        function backfill_parents()
        {
            global $wpdb;
            // find parents for post orphans
            foreach ($this->post_orphans as $child_id => $parent_id) {
                $local_child_id = $local_parent_id = false;
                if (isset($this->processed_posts[$child_id])) {
                    $local_child_id = $this->processed_posts[$child_id];
                }
                if (isset($this->processed_posts[$parent_id])) {
                    $local_parent_id = $this->processed_posts[$parent_id];
                }
                if ($local_child_id && $local_parent_id) {
                    $wpdb->update($wpdb->posts, array('post_parent' => $local_parent_id), array('ID' => $local_child_id), '%d', '%d');
                }
            }
            // all other posts/terms are imported, retry menu items with missing associated object
            $missing_menu_items = $this->missing_menu_items;
            foreach ($missing_menu_items as $item) {
                $this->process_menu_item($item);
            }
            // find parents for menu item orphans
            foreach ($this->menu_item_orphans as $child_id => $parent_id) {
                $local_child_id = $local_parent_id = 0;
                if (isset($this->processed_menu_items[$child_id])) {
                    $local_child_id = $this->processed_menu_items[$child_id];
                }
                if (isset($this->processed_menu_items[$parent_id])) {
                    $local_parent_id = $this->processed_menu_items[$parent_id];
                }
                if ($local_child_id && $local_parent_id) {
                    update_post_meta($local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id);
                }
            }
        }

Usage Example

 public function wpImportContents($file)
 {
     $wp_import = new WP_Import();
     $wp_import->fetch_attachments = false;
     // doesn matter
     // load data from saved option
     $wp_import->post_orphans = get_option('_cri_post_orphans', array());
     $wp_import->processed_posts = get_option('_cri_processed_posts', array());
     $wp_import->url_remap = get_option('_cri_url_remap', array());
     // the odd filter
     add_filter('import_post_meta_key', array($wp_import, 'is_valid_meta_key'));
     add_filter('http_request_timeout', array(&$wp_import, 'bump_request_timeout'));
     // start buffer
     ob_start();
     // parse file and gather data
     $wp_import->import_start($file);
     // map author
     $wp_import->get_author_mapping();
     wp_suspend_cache_invalidation(true);
     $wp_import->process_categories();
     $wp_import->process_tags();
     $wp_import->process_terms();
     $wp_import->process_posts();
     wp_suspend_cache_invalidation(false);
     // update incorrect/missing information in the DB
     $wp_import->backfill_parents();
     $wp_import->backfill_attachment_urls();
     $wp_import->remap_featured_images();
     // end has output, so buffer it out
     $wp_import->import_end();
     // ignore the output, call in buffer
     do_action('radium_after_content_import');
     ob_end_clean();
     // delete all attachment related stats
     foreach (array('_cri_post_orphans', '_cri_processed_posts', '_cri_url_remap') as $op) {
         delete_option($op);
     }
     return true;
 }
All Usage Examples Of WP_Import::backfill_parents