• 本帖最后由 yaoruisheng 于 2019-1-15 16:16 编辑 *

开门见山,我首先引用wp-load.php

  • include_once("../wp-load.php");复制代码

然后用wp_insert_post 这个函数直接发表一篇文章

  • $post_id = wp_insert_post( $post , $error);复制代码

其中$post这个变量,我打印出来的结果是

  • - array(8) {
  • ["post_author"]=>
  • int(1)
  • ["post_content"]=>
  • string(115) "**"
  • ["post_title"]=>
  • string(93) "谷歌,搜你所搜!"
  • ["post_status"]=>
  • string(7) "publish"
  • ["post_type"]=>
  • string(4) "post"
  • ["post_category"]=>
  • array(1) {
  • [0]=>
  • int(3)
  • }
  • ["tax_input"]=>
  • array(1) {
  • ["post_format"]=>
  • string(5) "video"
  • }
  • ["meta_input"]=>
  • array(1) {
  • ["gg_auto_id"]=>
  • string(11) "1VQbu3FcNHU"
  • }
  • }
  • 复制代码

问:为什么最终结果是文章发布了,但是只有标题,没有内容,并且文章分类也不对?

经过折腾,发现是权限问题,内容为空是wordpress过滤掉内容里的iframe标签,需要用kses_remove_filters()函数关闭过滤器。

文章的类别无法通过tax_input进行设置,由函数wp_insert_post源码https://developer.wordpress.org/reference/functions/wp_insert_post/ 可见:

  • - if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
  • wp_set_post_terms( $post_ID, $tags, $taxonomy );
  • }
  • 复制代码

只有登陆用户才可以wp_set_post_terms。