Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress使用PHP创建帖子_Php_Wordpress_Insert - Fatal编程技术网

Wordpress使用PHP创建帖子

Wordpress使用PHP创建帖子,php,wordpress,insert,Php,Wordpress,Insert,我想制作一个定制的PHP脚本,可以为wordpress制作一篇文章 这是我在官方页面上的代码,但它不起作用: require("wp-includes/post.php"); // Create post object $my_post = array( 'post_title' => "mytitle", 'post_content' => "mycontent", 'post_status' => 'publish', 'post_author

我想制作一个定制的PHP脚本,可以为wordpress制作一篇文章

这是我在官方页面上的代码,但它不起作用:

require("wp-includes/post.php");

// Create post object
$my_post = array(
  'post_title'    => "mytitle",
  'post_content'  => "mycontent",
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 1 )
);

wp_insert_post( $my_post );
错误是:

致命错误:在中调用未定义的函数get_current_user_id() /home/MyUser/public_html/wp includes/post.php,第2897行


我做错了什么?

问题可能是您没有作为后端用户登录。脚本正在尝试获取后端用户id。必须有用户登录才能创建帖子。每个帖子都需要一个用户ID,用于设置帖子的作者


另外,您是在插件内部还是在wordpress外部执行此脚本?

对于自定义PHP脚本,只需添加正确的include“wp load.PHP”而不是“post.PHP”

wp_insert_post()-返回新帖子的ID


确切地说:您正在调用一个不可用的函数。因此,永远不会加载
函数\u get\u user\u id()
。能否提供指向您正在使用的文档页面的链接?我猜您缺少一个或一些需要的文件()您是在WordPress安装之外进行此操作的吗?您不需要包含/需要任何WP-files。下面的示例是:Im以管理员身份登录(作者ID=1),此脚本以会话_start()开始;
require('wp-load.php');
//require("wp-includes/post.php");

// Create post
$my_post = [
  'post_title'    => "mytitle",
  'post_content'  => "mycontent",
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => [ 1 ]
];

wp_insert_post( $my_post );