• Hello,

    I have some problem with importing about 200k posts from a different source to wordpress. I createt a custom Plugin that should do this. I upload a JSON File with all the needed data in it. The Json looks like this: https://pastebin.com/JUzUexbL

    In the JSON File that should be proceeded are about 150 entrys of persons, some persons own up to 500 candles and 200 condolences. So about 100k posts, because every person, every candle and every condolence is a single post.

    I am doing this with this js-script that posts via admin-post to a proceed-script.

    The problem is, that i get about 90% a timeout back from ajax. So how could i imporove my solution approach?

    
        var $ = jQuery;
        
        $(document).ready(function() {
        
            $.getJSON(WP.jsonfile, function(data) {
        		var failed = [];
        		var flag = true;
                var count = -1;
                var total = data.length;
                var step = 100 / total;
        
                $(".my_progregg_bar .progress_percentage").text("0 / " + total);
        
                $.each(data, function(index, value) {
        
                    if (flag) {
                        $.ajax({
                            url: WP.ajax_url,
                            method: "POST",
                            async: true,
                            data: {
                                action: "epimport",
                                data: value
                            },
                            success: function(e) {
                                count++;
                                console.log(e);
        
                                $(".my_progregg_bar .inner").css({"width" : step*count + "%"});
                                $(".my_progregg_bar .progress_percentage").text(count+1 + " / " + total);
                            },
                            error: function(e) {
        						failed.push(index);
                                console.log(e);
                            }
                        });
                    } else {
        				count++;
        			}
                });
            });
        });

    PHP-Script:

    
       <?php
        
            $sterbefall = EP_STERBEFALL::setup($_POST["data"]);
        
            $sterbefall_post = wp_insert_post(array(
                "post_title" => $sterbefall->name,
                "post_status" => "publish",
                "post_type" => "sterbefall",
            ));
        
            $attachment = array(
                "post_mime_type" => "image/jpeg",
                "post_parent" => $sterbefall_post->ID,
                "post_title" => "Parte " . $sterbefall->name,
                "post_content" => "",
                "post_status" => "publish"
            );
        
            $attachment_id = wp_insert_attachment($attachment, $sterbefall->parte, $sterbefall_post);
        
            if (!is_wp_error($attachment_id)) {
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
        		wp_update_attachment_metadata( $attachment_id,  $attachment_data );
            }
        
        
        
            $attachment = array(
                "post_mime_type" => "image/jpeg",
                "post_parent" => $sterbefall_post->ID,
                "post_title" => "Portrait " . $sterbefall->name,
                "post_content" => "",
                "post_status" => "publish"
            );
        
            $portrait_id = wp_insert_attachment($attachment, $sterbefall->portrait, $sterbefall_post);
        
            if (!is_wp_error($portrait_id)) {
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        		$attachment_data = wp_generate_attachment_metadata( $portrait_id, $upload_file['file'] );
        		wp_update_attachment_metadata( $portrait_id,  $attachment_data );
            }
        
            update_field("death_die_date", $sterbefall->sterbedatum, $sterbefall_post);
            update_field("death_funeral_place", $sterbefall->ort, $sterbefall_post);
            update_field("death_parten", array($attachment_id), $sterbefall_post);
            update_field("death_portrait", $portrait_id, $sterbefall_post);
        
        
            foreach ($sterbefall->gedenkkerzen as $kerze) {
                wp_insert_post(array(
                    "post_title" => $kerze->text,
                    "post_status" => "publish",
                    "post_type" => "gedenkkerze",
                    "post_date" => $kerze->timestamp,
                    "post_parent" => $sterbefall_post
                ));
            }
        
            foreach ($sterbefall->kondolenzen as $kondolenz) {
                $kondolenz_id = wp_insert_post(array(
                    "post_title" => $kondolenz->name,
                    "post_status" => "publish",
                    "post_type" => "kondolenz",
                    "post_date" => $kondolenz->timestamp,
                    "post_parent" => $sterbefall_post
                ));
        
                update_field("condolence_text", $kondolenz->text, $kondolenz_id);
            }
        
        ?>
    
    
    • This topic was modified 5 years, 10 months ago by Jan Dembowski. Reason: Fixed formatting
    • This topic was modified 5 years, 10 months ago by Jan Dembowski.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Making a single HTTP request for each and every post to insert is going to be very, very slow. You should consider sending them in batches, or better yet, not sending them via AJAX at all.

    You have access to the server directly, so write some one-time PHP code to read in your JSON file there, and then loop through the posts on the server itself. Run that on the command line, not in an HTTP call, to avoid timeouts. To gain access to the WordPress functions from your PHP command line call, just include the wp-load.php file. It’ll load the rest of WordPress.

    Surrounding your loop, you will want to turn off some bits of WordPress to speed up the import process. Specifically, you want to call wp_suspend_cache_invalidation( true ); to stop the object cache from taking up too much time for all the inserts. You will probably also want to call wp_defer_term_counting( true ); and wp_defer_comment_counting( true ); to turn off the recount of comments and terms on insertions.

    Thread Starter mathishuettl

    (@mathishuettl)

    Thanks for this usefull tipp! I wrote a php import script now, everything works nearly perfect. I just have 1 problem. Everytime at the 110/535 entry the script suddenly stops without an warning/error or something. Is there a limit inside wordpress or what else could i do?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    No limits in WP there. Your server might be killing the process.

    Thread Starter mathishuettl

    (@mathishuettl)

    hmmm okay do you have an idea why?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Import 100k posts to WordPress’ is closed to new replies.