抽空整理出WordPress表关系,查询出文章列表
作者: 葛屹肃 | 日期: 2023-12-19 | 分类: 知识经验
前几天在后台清理文章,发现效率太低,于是抽空整了下wordpress表关系。我的wordpress似乎是4.2版本,一共有11张表,其关系并不复杂,基于自己的用途,整理了与文章相关的几张表关系。
以上结果是使用phpMyAdmin查询的,依次为文章标题、分类、状态、浏览率、发布时间。SQL如下:
select post_title,te.name,(case post_status when ‘publish’ then ‘已发布’ when ‘draft’ then ‘草稿’ when ‘auto-draft’ then ‘草稿’ when ‘private’ then ‘私有’ when ‘future’ then ‘定时’ when ‘pending’ then ‘待审’ when ‘inherit’ then ‘修订版本’ else ‘其他’ end) post_status,(meta_value+0) AS views
from wp_posts p,wp_term_relationships r,wp_term_taxonomy ta,wp_terms te,wp_postmeta po
where r.object_id=p.id
and r.term_taxonomy_id=ta.term_taxonomy_id
and ta.term_id=te.term_id
and te.term_id in (1,2,5,12,50) — 分类ID
and po.meta_key=’views’
and po.post_id=p.id
order by post_date desc
懂SQL的一看就了解,有一点要注意,就是分类与标签都在wp_terms表中,因此,要指定分类的ID。
了解表关系后,那么删除文章较为方便,如将分类ID为2,浏览量低于299的文章修改其状态为草稿,这样在网站上就看不到其文章。想删除什么文章,改下条件即可。
update wp_posts
set post_status=’draft’
where id in
(
select id FROM
(
select p.id
from wp_posts p,wp_term_relationships r,wp_term_taxonomy ta,wp_terms te,wp_postmeta po
where r.object_id=p.id
and r.term_taxonomy_id=ta.term_taxonomy_id
and ta.term_id=te.term_id
and te.term_id=2 — 指定分类ID
and po.meta_key=’views’
and po.post_id=p.id
and (meta_value+0)<200 — 浏览量低于200
) a
)
SQL看起来有点复杂,这是由于MySQL特性,更改或删除表时,不能在子查询中更新表并直接从同一表中进行选择,因此,在子查询中多嵌入一套。
为了便于查询,我将查询文章SQL生成了视图,视图生成方式可参阅《使用phpMyadmin创建视图的方法》文章。
好了,写完收工
文章链接:https://www.geyisu.com/3338.html