首页 > 解决方案 > 使用 wp_insert_post() 添加批量帖子获取内部服务器错误

问题描述

我编写了一个批量导入脚本,在其中wp_insert_post()使用分类名称导入带有函数的帖子。基本上有4000个城市,我想将它们添加为帖子,例如:

ETC...

但是在添加了几千个帖子后,我遇到了内部服务器错误。我预计添加的帖子总数为 16,000,000 [4,000 * 4,000]

这是代码:

<?php
require_once "wp-load.php";
ini_set("memory_limit",-1);
set_time_limit(0);
ignore_user_abort(true);

$term_query = new WP_Term_Query( array( 'taxonomy' => 'cities' ) );
if ( ! empty( $term_query->terms ) ) {
    wp_defer_term_counting( true );
    define( 'WP_IMPORTING', true );
    foreach ( $term_query ->terms as $term ) {
    $city1 = $term->name;
        foreach ( $term_query ->terms as $inner_city ) {
            $city2 = $inner_city->name;
            $postTitle = $city1 . ' to ' . $city2;
            $postSlug  = $city1 . '-to-' . $city2;
            $LowerPostSlug = strtolower($postSlug);
            $post_args = array(
                'post_title' => $postTitle,
                'post_status' => 'publish',
                'post_name' => $postSlug,
                'post_author' => $user_ID,
                'post_type' => 'conversion',
            );
        $found_post_title = get_page_by_title( $postTitle, OBJECT, 'city_post' );
        $found_post_id = $found_post_title->ID;

        if ( FALSE === get_post_status( $found_post_id ) ):
            $returned_post_id = wp_insert_post( $post_args );
            update_field( 'city_1', $city1, $returned_post_id ); # SET CUSTOM FIELD FOR city 1
            update_field( 'city_2', $city2, $returned_post_id ); # SET CUSTOM FIELD FOR city 2
            echo $postTitle . '</br>';
        endif;
        }
    }
    wp_defer_term_counting( false );
}

标签: phpwordpress

解决方案


推荐阅读