最近项目需要,分析了一下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

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

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

/**
* Display post thumbnail meta box.
*
* @since 2.9.0
*
* @param WP_Post $post A post object.
*/
function post_thumbnail_meta_box( $post ) {
$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); //获取特色图像对应的ID
echo _wp_post_thumbnail_html( $thumbnail_id, $post->ID ); //输出HTML
}

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

/**
* Retrieve post meta field for a post.
*
* @since 1.5.0
*
* @param int $post_id Post ID.
* @param string $key Optional. The meta key to retrieve. By default, returns
* data for all keys. Default empty.
* @param bool $single Optional. Whether to return a single value. Default false.
* @return mixed Will be an array if $single is false. Will be value of meta data
* field if $single is true.
*/
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata('post', $post_id, $key, $single);
}

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

/**
* Retrieve metadata for the specified object.
*
* @since 2.9.0
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object.
* @param bool $single Optional, default is false.
* If true, return only the first value of the specified meta_key.
* This parameter has no effect if meta_key is not specified.
* @return mixed Single metadata value, or array of values
*/
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
} $object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
//echo $object_id . ':' . $meta_type . '-' . $meta_key . '-' . $single . '<br />';
/**
* Filters whether to retrieve metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, 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 这个函数。

/**
* Update the metadata cache for the specified objects.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
* @return array|false Metadata cache for the specified objects, or false on failure.
*/
function update_meta_cache($meta_type, $object_ids) {
global $wpdb; if ( ! $meta_type || ! $object_ids ) {
return false;
} $table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
} $column = sanitize_key($meta_type . '_id'); if ( !is_array($object_ids) ) {
$object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
$object_ids = explode(',', $object_ids);
} $object_ids = array_map('intval', $object_ids); $cache_key = $meta_type . '_meta';
$ids = array();
$cache = array();
foreach ( $object_ids as $id ) {
$cached_object = wp_cache_get( $id, $cache_key );
if ( false === $cached_object )
$ids[] = $id;
else
$cache[$id] = $cached_object;
} if ( empty( $ids ) )
return $cache; // Get meta info
$id_list = join( ',', $ids );
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
echo "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC" . "<br />";
$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); if ( !empty($meta_list) ) {
foreach ( $meta_list as $metarow) {
$mpid = intval($metarow[$column]);
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value']; // Force subkeys to be array type:
if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
$cache[$mpid] = array();
if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
$cache[$mpid][$mkey] = array(); // Add a value to the current pid/key:
$cache[$mpid][$mkey][] = $mval;
}
} foreach ( $ids as $id ) {
if ( ! isset($cache[$id]) )
$cache[$id] = array();
wp_cache_add( $id, $cache[$id], $cache_key );
} return $cache;
}

关键的语句在这里

SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (21) ORDER BY meta_id ASC

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

本文内容基于 Wordpress 4.8版本

参考资料:

1、如何设置Wordpress的特色图像

2、WordPress数据库表及字段详解

3、理解和利用 WordPress 中的元数据(Metadata)

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

  1. wordpress中文章发布时间不显示?用get_the_date代替the_date

    今天发现,在主题中部分地方使用the_date函数来显示文章发布的时间时,竟然发生不显示时间的情况,再仔细看了一下这些文章,有些都是经过几次修改和保存的,可能是由于the_date只是显示文章第一次发 ...

  2. 如何设置WordPress文章特色图像(Featured Image)

    WordPress的特色图像(Featured Image)是一个很方便的功能,过去为了给每篇文章设置一个缩略图,我们需要用脚本去匹配文章中的第一张或者最后一张图片,或者通过附件方式获取图片,有了特色 ...

  3. WordPress发布文章前强制要求上传特色图像

    如果你的网站需要给每篇文章设置特色图像才能达到理想的显示效果,而且允许其他用户在后台发布文章的,那么您可能需要强制要求他们给文章上传特色图像,否者就无法发布.Require Featured Imag ...

  4. 为WordPress某个文章添加额外的样式

    如需把css直接写在某文章,把下面代码放如function.php /* 为特定文章添加特定css最简单的方式. */ /*添加自定义CSS的meta box*/ add_action('admin_ ...

  5. wordpress获取文章特色图像路径函数wp_get_attachment_image_src()

    特色图像是wordpress主要的文章缩略图功能,几乎全部wordpress模板都使用或支持特色图像.今天介绍的wp_get_attachment_image_src()函数就是获取文章特色图像路径的 ...

  6. WordPress 获取文章内容页特色图像地址

    WordPress获取特色图像地址主要需要用到两个函数get_post_thumbnail_id和wp_get_attachment_image_src.下面是分别获取小.中.大.完整.指定图片规格的 ...

  7. PHP模版引擎twig wordpress中调用文章第一张图片

    wordpress当文章没有添加Featured media的时候, 就调用文章第一张图片, 调用的wordpress代码函数为: <?php echo catch_that_image(); ...

  8. WordPress获取特色图像的链接地址

    为什么要获取WordPress的特色图像呢? 这主要是因为,我们已经写好了静态模板文件,只有获取WordPress特色图像地址插入进去就可以了,非常方便. 还有就是有的时候,我们需要设置图片的宽度为1 ...

  9. 什么是WebP以及如何在WordPress中使用WebP图像

    图像通常是缓慢加载网页的最大原因之一.它们不仅减慢了加载时间,而且还可以占用服务器上的大量空间和资源.仔细选择文件类型并压缩它们有助于降低加载速度,但它们只能在图像质量受损之前进行优化.另一种选择是使 ...

随机推荐

  1. day2编写购物商城(1)

    作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够     具体实现了如下功能: 1.可购买的商品信息显示 2.显示购物车内的商品信息.数量.总金额 3.购物车内的商品数量进行增加.减少和商 ...

  2. 站点的安全防范都是后端的职责?非也,Web前端安全同样不可忽视

    前言 随着网络的快速普及,网络安全问题的受害者不再只是政府.企业等集体,每一个接触网络的普通人都有可能成为网络攻击的受害者.随着网络的普及,黑客进行网络攻击的手段越来也多,越来越复杂.以网站的攻击为例 ...

  3. loadrunner学习笔记之参数设置

    一.关于参数的定义 在你录制程序运行的过程中,脚本生成器自动生成由函数组成的用户脚本.函数中参数的值就是在录制过程中输入的实际值. 例如,你录制了一个Web应用程序的脚本.脚本生成器生成了一个声明,该 ...

  4. react-native第一次开发记录

    1.安装指定版本 react-native init demo --verbose --version 0.41.0 2.更新依赖包 npm install -g npm-check-updates ...

  5. CSUOJ 1162 Balls in the Boxes 快速幂

    Description Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of th ...

  6. 批量ssh登录,获取操作系统、CPU、内存、硬盘信息<shell>

    说明:该脚本读取machine.txt文件中的机器名,然后批量ssh登录,获取每台机器的操作系统,CPU,内存,硬盘等信息. 使用方法:将文件保存为sh,chmod +x filename 为该sh文 ...

  7. shell if [ -d filename]

    [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ ...

  8. 八皇后II

    用一个数组state记录已经选择的每一行皇后所在的位置,DFS count = 0 N = 8 state = [0]*N def dfs(row): global count for col in ...

  9. c++ 时间相关的类型

    关于时间转换可以参考以下博客: https://www.jianshu.com/p/80de04b41c31 https://www.cnblogs.com/qicosmos/p/3642712.ht ...

  10. CF 277.5 C.Given Length and Sum of Digits.. 构造

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...