1

I am trying to insert a post programmatically. A file is placed in the same directory where WordPress is installed. When I try to include wp-load.php, it is redirecting to wp-admin/install.php and says WordPress is already installed.

Here is my code:

function insertPost(){

    require_once( 'wp-load.php' );    

    $postarr['ID'] = 0;
    $postarr['post_author'] = 1;
    $postarr['post_date'] = '2017-02-24';
    $postarr['post_date_gmt'] = '2017-02-24';
    $postarr['post_content'] = 'Test Contect';
    $postarr['post_title']  = 'Test Title';
    $postarr['post_excerpt']  = 'Test Excerpt';
    $postarr['post_status']  = 'publish';
    $postarr['post_type'] = 'post';
    $postarr['post_category'][0]= 33; 
    $postarr['tax_input']['post_tag'] = 'TestTag';

    $postarr['meta_input']['label'] = 'Tets Label';
    $postarr['meta_input']['Released'] = 'Nov 4, 2016';
    $postarr['meta_input']['slide_template'] = 'default';
    $postarr['meta_input']['_wpb_vc_js_status'] = 'true';  

    $arrID = wp_insert_post( $postarr, $wp_error = false );
    print_r($arrID);        
 }

 insertPost();

I am using WordPress 4.7.2. Any help would be appreciated. Thanks!

  • 1
    It should work just fine. Make sure wp-load.php is actually in the PWD. echo __FILE__ to check if it is. Also, there is no authentication in your function, check authentication somewhere, otherwise it may pose a security risk. – Fayaz Feb 24 '17 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.