Wordpress中文章的特色图像Featured Image究竟存在哪里?
Wordpress中文章的特色图像Featured Image究竟存在哪里?

Wordpress中文章的特色图像Featured Image究竟存在哪里?

最近项目需要,分析了一下Wordpress的特色图像 Feature Image的上传、保存方式,这一分析觉得Wordpress的数据结构设计还真是有想法。

先简单说一下结论:

Wordpress中图像物理文件保存在  wp-content/uploads 目录下,相关信息保存在 wp_posts 表中。post_type 是 attachment,post-mime-type 是 image/png。通过 post_parent 与文章关联。

我原来以为图片信息会有单独的表存放,没想到都放在 wp_posts 中,于是分析了这样做有什么好处。

wp_posts 表

首先来看看 wp_posts 表。该表用来存放文章信息,如文章标题、正文、摘要、作者、发布时间、访问密码、评论数、修改时间、文章地址(非静态化之前的,带?和数字ID)等。这些属性都是与文章相关的,同时根据 post_type的不同,该表还能用来存储特色图像 Featured Image。

字段 含义
ID 自增唯一ID
post_author 对应作者ID
post_date 发布时间
post_date_gmt 发布时间(GMT+0时间)
post_content 正文
post_title 标题
post_excerpt 摘录
post_status 文章状态(publish
comment_status 评论状态(open
ping_status PING/Trackback状态(open/closed)
post_password 文章密码
post_name 文章缩略名
to_ping 要引用的URL链接
pinged 已经PING过的链接
post_modified 修改时间
post_modified_gmt 修改时间(GMT+0时间)
post_content_filtered 未知
post_parent 父文章,主要用于PAGE
guid 文章的一个链接。注意:不能将GUID作为永久链接(虽然在2.5之前的版本中它的确被当作永久链接),也不能将它作为文章的可用链接。GUID是一种独有的标识符,只是目前恰巧成为文章的一个链接。
menu_order 排序ID
post_type 文章类型(post/page/attachment/revision等)
post_mime_type MIME类型
comment_count 评论总数

由此可以看到,Wordpress 利用 post_type 可以在该表中存储草稿、文章、页面、附件等丰富的信息,一张表就搞定了。

wp_postmeta 表

与这张表相关联的,还有一个 wp_postmeta 表,用来存储与文章相关的元数据。这个表的表结构比较简单。

字段 含义
meta_id 元数据记录的ID。
post_id 就是元数据相关联的post,用户(user),评论(comment)的ID。
meta_key 元键(meta key)(这个值在不同的记录中经常是重复的)。
meta_value 元值(meta value)(往往是唯一的)。

那么,对于一个文章,是如何来获取特色图像 Featured Image的,下面来看一下。在后台的文章编辑界面,特色图像显示在这个位置。

Wordpress中文章的特色图像Featured Image究竟存在哪里?插图

对应的后台代码是 wp-admin/includes/meta-boxes.php

继续找 get_post_meta ,在wp-includes/post.php中。

继续找 get_metadata 在 wp-includes/meta.php 中。

, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 3.1.0
*
* @param null|array|string $value The value get_metadata() should return – a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param bool $single Whether to return only the first value of the specified $meta_key.
*/
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}

$meta_cache = wp_cache_get($object_id, $meta_type . ‘_meta’);

if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}

if ( ! $meta_key ) {
return $meta_cache;
}

if ( isset($meta_cache[$meta_key]) ) {
if ( $single )
return maybe_unserialize( $meta_cache[$meta_key][0] );
else
return array_map(‘maybe_unserialize’, $meta_cache[$meta_key]);
}

if ($single)
return ”;
else
return array();
}

这个函数中会根据 $meta_key 和 $object_id 、$meta_type 取出特色图像对应的ID。为了避免重复读取数据库,这里用了缓存,我们可以看 update_meta_cache 这个函数。

关键的语句在这里

可以看到结果如下,_thumbnail_id 对应的就是 wp_posts 中的记录ID,从而可以取到图片对应的地址和相关信息。

Wordpress中文章的特色图像Featured Image究竟存在哪里?插图1

本文内容基于 Wordpress 4.8版本

 

 

Visits: 3

发表回复