将约10万篇文章导入Wordpress

时间:2019-01-11 22:14:57

标签: ajax wordpress

从其他来源向wordpress导入大约200k帖子时遇到一些问题。我创建了一个应该执行此操作的自定义插件。我上传了一个JSON文件,其中包含所有需要的数据。 Json看起来像这样:https://pastebin.com/JUzUexbL

在应该处理的JSON文件中,大约有150个人的条目,有些人拥有多达500支蜡烛和200枚慰问品。大约有10万个帖子,因为每个人,每个蜡烛和每一个慰问都是一个帖子。

我正在使用此js脚本来完成此任务,该js脚本通过admin-post发布到proceed-script。

问题是,我从ajax返回大约90%的超时。那么我该如何改善我的解决方案呢?

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脚本:

<?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);
    }

?>

0 个答案:

没有答案