wordpress 使用mysql添加文章_WordPress代码插入文章函数:wp_insert_post

wp_insert_post 是用于在 WordPress 中插入或更新文章的关键函数。它处理文章对象,包括设置标题、内容、状态等,并返回新创建文章的ID。此函数会自动过滤和检查输入的安全性,无需额外的清理工作。使用时需提供文章的属性数组,如标题、内容、状态等。例如,创建一篇文章时,可以设置文章标题、内容和状态,然后调用 wp_insert_post 函数将其保存到数据库。
摘要由CSDN通过智能技术生成

【描述】

该函数可在数据库中插入文章(及页面)。它可以进行处理变量,检查操作,填充日期/时间等缺失变量等工作。该函数以对象作为变量,返回已创建文章的编号(出错时返回0)。

【使用方法】

【参数】

$post

(array) (必需) 一个文章对象. 与数据库wp_posts表中的字段一一对应

默认: 无

重要: 如果设置$post[‘ID’]的值,将不会创建 这个ID的文章. 设置这个值将会更新这个ID的文章. 简单的说,创建一个文章 $post[‘ID’] 必须为空或不设置这个值.

$post = array(

'ID' => [ ] //需要更新的文章编号

'menu_order' => [ ] //如果新文章是页面,设置显示顺序

'comment_status' => [ 'closed' | 'open' ] // 评论的状态,'closed'关闭评论.

'ping_status' => [ 'closed' | 'open' ] // ping的状态,'closed' 关闭 pingbacks和trackbacks

'pinged' => [ ? ] //该文章被ping到的地址

'post_author' => [ ] //作者编号

'post_category' => [ array(, <...>) ] //文章归类数组

'post_content' => [ ] //文章内容,必填

'post_date' => [ Y-m-d H:i:s ] //文章编辑日期

'post_date_gmt' => [ Y-m-d H:i:s ] //文章编辑GMT日期

'post_excerpt' => [ ] //摘要信息

'post_name' => [ ] // (slug) 文章别名

'post_parent' => [ ] //新文章的父文章编号

'post_password' => [ ? ] //文章浏览密码

'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的状态

'post_title' => [ ] //文章标题,必填

'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章类型:文章、页面、链接、菜单、其他定制类型

'tags_input' => [ ', , <...>' ] //标签字符串

'to_ping' => [ ? ] //该文章需要ping到的地址

'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // 附加注释数组

);

$wp_error

(布尔型) (可选) 失败时是否返回WP_Error对象

默认: false

【返回的值】

若文章成功加入数据库,返回文章编号。否则返回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 );

【安全】

函数会自动过滤和检查文章信息的合法性,不需要用户自己来额外处理

【源码位置】

wp_insert_post() 位于 wp-includes/post.php

/**

* Insert a post.

*

* If the $postarr parameter has 'ID' set to a value, then post will be updated.

*

* You can set the post date manually, but setting the values for 'post_date'

* and 'post_date_gmt' keys. You can close

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值