WordPress 通过文章标题获取文章 ID 等 post 信息

张子凡 WordPress优化 2019-08-14 14:13:14 阅读(...) 评论(2)

WordPress 可以通过文章 ID 获取到指定文章的数据,也可以通过 URL 获取文章 ID,但是最近子凡在开发一个泪雪网的批量数据理解功能,需要将相关标题的文章 URL 做批量提交,那么就需要用到 WordPress 的一个自带函数 get_page_by_title。

WordPress get_page_by_title

WordPress 默认也已经提供了可以通过文章标题就能够快速获取到文章信息的函数“get_page_by_title”,作用和 get_post 函数功能差不多,只是查询的条件不一样,返回的结果其实就是 get_post 处理过的数据。

1
2
3
4
5
6
7
8
9
10
//格式
get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' )
 
$page_title 页面标题
$output 返回的类型格式,例如 OBJECT,ARRAY_A 或 ARRAY_N 选一个,默认是 OBJECT
$post_type 文章类型,默认为 page 页面,查询文章为 post
 
//WordPress 通过文章标题获取 post 信息
$post = get_page_by_title('这里就是文章的标题', OBJECT, 'post');
$postid = $post->ID; //文章 ID

详细的 post 返回字段包括:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ID          int     文章 ID
post_author     string  作者 user ID
post_name   string  文章别名
post_type   string  文章类型
post_title  string  文章标题
post_date   string  文章时间,格式为: 0000-00-00 00:00:00
post_date_gmt   string 文章 gmt 时间,格式为,: 0000-00-00 00:00:00
post_content    string  文章内容
post_excerpt    string  文章摘要
post_status     string  文章状态
comment_status  string  评论状态,开启或者关闭: { open, closed }
ping_status     string  ping 状态 { open, closed }
post_password   string  文章密码
post_parent     int     父 ID,默认是 0
post_modified   string  文章修改时间,格式为: 0000-00-00 00:00:00
post_modified_gmt   string  文章修改时间,格式为: 0000-00-00 00:00:00
comment_count   string  文章评论统计
menu_order  string  好像没什么用,默认是 0

好啦,简单的做个记录和分享,相信对于一些新人 WordPress 开发者会有一点点帮助吧,可以竟可能的避免重复造轮子的情况,利用好 WordPress 已经提供好的函数,可以让开发效率事半功倍。

更多关于WordPress优化及疑问可以添加QQ群:255308000

除非注明,否则均为泪雪博客原创文章,禁止任何形式转载

本文链接:https://zhangzifan.com/wordpress-get_page_by_title.html

留言评论

登录 后留言