【问题标题】:wp_insert_post error: " Content, title, and excerpt are empty."wp_insert_post 错误:“内容、标题和摘录为空。”
【发布时间】:2013-09-12 19:10:37
【问题描述】:

我正在尝试通过 PHP 将 WooCommerce 产品插入 WordPress 数据库。 WooCommerce 产品只是 post-type=product 的帖子,所以我认为这应该可行:

$mypost = array('post-title' => $secondexplosion[0], 'post-type' => 'product',
   'post-status' => 'publish', 'post-content' => $secondexplosion[1],
   'post-excerpt' => '');
$postid = wp_insert_post($mypost, true);

$secondexplosion 是一个包含帖子标题和帖子内容的字符串数组;我检查了一下,它不包含空值或任何有问题的东西。那么,为什么 wp_insert_post 返回错误“内容、标题和摘录为空”?非常感谢!

【问题讨论】:

  • 您应该使用下划线而不是破折号(即post_content 而不是post-content)。如果你改变这些应该可以工作

标签: php wordpress woocommerce


【解决方案1】:

Codex for wp_insert_post 有很多有用的信息:

$post = array(
  'ID'             => [ <post id> ] //Are you updating an existing post?
  'menu_order'     => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
  'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
  'ping_status'    => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
  'pinged'         => [ ? ] //?
  'post_author'    => [ <user ID> ] //The user ID number of the author.
  'post_category'  => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
  'post_content'   => [ <the text of the post> ] //The full text of the post.
  'post_date'      => [ Y-m-d H:i:s ] //The time post was made.
  'post_date_gmt'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
  'post_excerpt'   => [ <an excerpt> ] //For all your post excerpt needs.
  'post_name'      => [ <the name> ] // The name (slug) for your post
  'post_parent'    => [ <post ID> ] //Sets the parent of the new post.
  'post_password'  => [ ? ] //password for post?
  'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post.
  'post_title'     => [ <the title> ] //The title of your post.
  'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type
  'tags_input'     => [ '<tag>, <tag>, <...>' ] //For tags.
  'to_ping'        => [ ? ] //?
  'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. 
);  

看起来您的字段中出现了拼写错误;预期的字段名称是 post_title post_type post_status post_contentpost-excerpt - 带有下划线,而不是破折号。

【讨论】:

  • 哇,我不敢相信我用错字浪费了你的时间。对不起,但谢谢你指出来!
  • 我们都做过同样的事情——有时只需要多一双眼睛就能看到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2019-12-19
  • 2017-11-22
  • 1970-01-01
  • 2015-05-02
  • 2019-10-22
相关资源
最近更新 更多