php - Getting error when using wp_insert_post()

admin2025-01-08  4

When I use wp_insert_post() I get a white page that says "Are you sure you want to do this?". I get no PHP errors. I am using this outside of wordpress and have wp-load.php as well as /wp-admin/includes/file.php included.

My code looks like this.

require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
$imagePost = array(
        'post_title' => "Test",
        'post_content' => 'Content added later.',
        'post_status' => 'publish',
        'post_author' => 1,
        'tags_input' => "'test'",
        'post_category' => array(1),
        'post_type' => 'post'
);

$postID = wp_insert_post($imagePost, true);

Any help is much appreciated.

When I use wp_insert_post() I get a white page that says "Are you sure you want to do this?". I get no PHP errors. I am using this outside of wordpress and have wp-load.php as well as /wp-admin/includes/file.php included.

My code looks like this.

require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
$imagePost = array(
        'post_title' => "Test",
        'post_content' => 'Content added later.',
        'post_status' => 'publish',
        'post_author' => 1,
        'tags_input' => "'test'",
        'post_category' => array(1),
        'post_type' => 'post'
);

$postID = wp_insert_post($imagePost, true);

Any help is much appreciated.

Share Improve this question asked Sep 7, 2013 at 14:09 RambomstRambomst 1231 silver badge9 bronze badges 3
  • 1 That is a permissions error. It occurs when you try to access an admin page that you don't have permission to access. Can you please explain the context and post more code. While what you are doing is almost certainly pretty sketchy, it does work when I test it, though $_SERVER['DOCUMENT_ROOT'] was not correct on my server so I had to edit that. – s_ha_dum Commented Sep 7, 2013 at 15:33
  • A better thing you can do is use a web-service, where you can interact with your worpdress site, "API like style" – jepser Commented Sep 7, 2013 at 20:58
  • For what I am doing the XML-RPC won't do the job. If it is a permission error what do I need to do in order to be able to use this? It doesn't matter if it is sketchy, it isn't publicly available. – Rambomst Commented Sep 8, 2013 at 1:33
Add a comment  | 

1 Answer 1

Reset to default 0

Just add this code

    $post_id = wp_insert_post($arg);
    if(!is_wp_error($post_id)){
      //the post is valid
    }else{
      //there was an error in the post insertion, 
      echo $post_id->get_error_message();
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268059a1244.html

最新回复(0)