WordPress插入或更新文章函数wp_insert_post()

本文详细介绍了如何使用WordPress内置的wp_insert_post()函数来插入或更新文章。函数接受一个包含文章信息的数组,并返回文章ID。文章状态、类型、作者、分类、内容等关键参数都在数组中定义。示例代码展示了如何创建一篇新文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用WordPress自带的文章插入函数wp_insert_post();

函数基本用法

<?php wp_insert_post( $post, $wp_error ); ?>

函数参数详解

$post

要插入或更新的文章对象,和数据库中的wp_posts数据表中的字段一一对应;

$wp_error

默认:false

文章插入或更新失败时是否返回WP_Error对象

其中$post对象具体参数如下:

$post = array(
    'ID' => [ <post id> ] //需要更新的文章编号,如果不设置,则为插入一篇新文章
    'menu_order' => [ <order> ] //如果新文章是页面,设置显示顺序
    'comment_status' => [ 'closed' | 'open' ] // 评论的状态,'closed'关闭评论.
    'ping_status' => [ 'closed' | 'open' ] // ping的状态,'closed' 关闭 pingbacks和trackbacks
    'pinged' => [ ? ] //该文章被ping到的地址
    'post_author' => [ <user ID> ] //作者编号
    'post_category' => [ array(<category id>, <...>) ] //文章归类数组
    'post_content' => [ <the text of the post> ] //文章内容,必填
    'post_date' => [ Y-m-d H:i:s ] //文章编辑日期
    'post_date_gmt' => [ Y-m-d H:i:s ] //文章编辑GMT日期
    'post_excerpt' => [ <an excerpt> ] //摘要信息
    'post_name' => [ <the name> ] // (slug) 文章别名
    'post_parent' => [ <post ID> ] //新文章的父文章编号
    'post_password' => [ ? ] //文章浏览密码
    'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的状态
    'post_title' => [ <the title> ] //文章标题,必填
    'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章类型:文章、页面、链接、菜单、其他定制类型
    'tags_input' => [ '<tag>, <tag>, <...>' ] //标签字符串
    'to_ping' => [ ? ] //该文章需要ping到的地址
    'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // 附加注释数组
);

函数返回值

如果文章成功插入或更新,返回文章编号。否则返回0.

函数使用方法举例

$my_post = array(
    'post_title' => 'My post',
    'post_content' => 'This is my post.',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_category' => array(8,39)
);
wp_insert_post( $my_post );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yu-Knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值