php - Mysql SELECT自定义字段(post_meta)用于自定义帖子类型
问题描述
我正在尝试查询 Wordpress 帖子。
我创建了一个自定义帖子类型People
。每个people
帖子都有自定义字段name
、age
、location
等birthday
。自定义字段是使用高级自定义字段插件创建的。
我想查询自People
定义帖子类型的所有自定义字段。
我想要这样的输出:
+----+-------------+-----------+--------+-----------------------------+----------+-----+
| id | post_title | name | age | location | birthday |
+----+-------------+-----------+--------+-----------------------------+----------+-----+
| 1 | SAMPLE | some_name | XX | sample_location | 10/07/1980 |
| 1 | SAMPLE | some_name | XX | sample_location | 10/07/1980 |
| 1 | SAMPLE | some_name | XX | sample_location | 10/07/1980 |
| 1 | SAMPLE | some_name | XX | sample_location | 10/07/1980 |
| 1 | SAMPLE | some_name | XX | sample_location | 10/07/1980 |
+----+-------------+-----------+--------+-----------------------------+----------+-----+
这样做的正确语法是什么?
我努力了:
SELECT *
FROM `wp_posts` , `wp_postmeta`
WHERE `post_type` = 'people'
但这列出了所有 wordpress 常规帖子字段。
有人可以帮忙吗?
解决方案
这是任何可能有相同问题的人的解决方案!
SELECT posts_people.ID AS people_ID,
posts_people.post_title AS people_post_title,
(select meta_value from wp_postmeta where meta_key = 'name' AND post_id = posts_people.ID) as name,
(select meta_value from wp_postmeta where meta_key = 'age' AND post_id = posts_people.ID) as age,
(select post_title from wp_posts where ID = SUBSTRING_INDEX(SUBSTRING_INDEX((select meta_value from wp_postmeta where meta_key = 'location' AND post_id = posts_people.ID),'";',1),':"',-1)) as location,
(select meta_value from wp_postmeta where meta_key = 'birthday' AND post_id = posts_people.ID) as stelexos_kinito
FROM wp_posts AS posts_people
WHERE post_type = 'people' and post_status = 'publish'
推荐阅读
- google-cloud-platform - Google Cloud Build 不缓存自定义构建步骤?
- android - 如何在 Android 中提取 PDFTron 中的注释并保存到数据库?
- kotlin - onPlaceSelected 内部的方法不执行
- spring - Spring Batch 中的数据库锁定
- python - 使用 QPainter 覆盖两个具有 alpha 值的像素图
- javascript - Electron + xmpp.js:如何调试 SSL 自签名错误消息?
- c# - JsonConvert.DeserializeObject
返回空列表 - java - 合并排序逻辑错误
- javascript - 为什么在内联函数中使用时出现“no-unused-var”错误
- python - 将时间序列数据帧中的异常值重置为 3 SD