• Resolved hinhthoi

    (@hinhthoi)


    Hi,
    I’m trying to use wp_insert_post from an external php file. This file works fine if there is no loop

    <?php
    require('wp-blog-header.php');
    $tmpstr = array(
                  'ID' => 1,
                  'post_title' => $title,
                  'post_content' => $post content,
                  'post_status' => 'publish',
                  'post_author' => '1',
                  'post_type' => $type
               );
    wp_insert_post($tmpstr);
    ?>

    However, when i put a loop,
    `<?php
    for ($i=0;$i<10,$i++) {
    require(‘wp-blog-header.php’);
    $tmpstr = array(
    ‘ID’ => 1,
    ‘post_title’ => $title[$i],
    ‘post_content’ => $post content[$i],
    ‘post_status’ => ‘publish’,
    ‘post_author’ => ‘1’,
    ‘post_type’ => $type
    );
    wp_insert_post($tmpstr);
    }
    ?>
    It insert only 1 time into mysql database, then it stops
    Could anyone please help me to make it loop for 10 times, so that it can insert 10 entries?
    Thank you in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • try:

    require(ABSPATH.'wp-blog-header.php');

    wp_insert_post might be changing the relative url to the wp-admin directory

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Frumph
    Thank you for your help. But can you elaborate more about this? The path to the wp-blog-header.php is /home/myusername/public_html,
    so should it be
    require(‘/home/myusername/public_html/wp-blog-header.php’); ?

    I have tested that but it still insert only one entry

    was just a thought, otherwise it could be ‘exiting’ out of the function/loop with an error, have you checked your php error logs?

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi, could you tell me how to check the php logs? Since i’m new to php stuffs. It seems like this information is not accessible from a shared server, is it?

    That you will have to ask your hosting about.

    Mine on Cirtex hosting is in a file called error_log .. other peoples are in a /logs directory. It all depends on your hostings setup to handle error logs.

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Frumph, thanks for your help. I have resolve the problem by putting the require command outside of the loop

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘require('wp-blog-header.php') does not work in a loop’ is closed to new replies.