• I’m creating a plugin to import posts from a CSV file. The plugin is creating posts, but a random number of posts are created with an infinite loop.

    I have checked the while loop and for loop, they are working fine, but I think the problem is with the action hook.

    How could I fix this?

    My code is:

    function pfewrite_post(){
    $options = get_option(‘theme_options’);
    $path = $options[‘excelurl’];
    $file = fopen($path, ‘r’);
    $post = array();

    while ($row = fgetcsv($file)) {
    if ($header === null) {
    $header = $row;
    continue;
    }
    $all_rows[] = array_combine($header, $row);
    }

    foreach ( $all_rows as $rows ){

    $post_information = array(
    ‘post_title’ => $rows[‘Document ID’],
    ‘post_content’ => ‘description’,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘post_category’ => ‘Report’,
    ‘post_author’ => ‘VR’
    );
    $post_id = wp_insert_post($post_information);

    }

    }add_action(‘init’,’pfewrite_post’ );

  • The topic ‘creating posts from excel random number of posts are created’ is closed to new replies.