百度 | 神马 | 搜狗 | 技术文档 | 学习资料分享 - 记录帝国CMS及运维技术的点点滴滴
你的位置:首页 > 实用IT技术 » 正文

【SEO优化】WordPress 通过文章标题获取文章 ID 等 post 信息

发布时间:2020-09-17

作品分类:SEO优化  文章  标题  获取  文章  获取  标题  信息  WordPress

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

WordPress 可以通过文章 ID 获取到指定文章的数据,也可以通过 URL 获取文章 ID,但是最近IT131在开发一个泪雪网的批量数据理解功能,需要将相关标题的文章 URL 做批量提交,那么就需要用到 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 已经提供好的函数,可以让开发效率事半功倍。

Top