没有wp函数的Wordpress自定义元查询(wo.wp_query())

时间:2014-09-10 14:11:40

标签: mysql wordpress select title meta

我必须在不使用wp_query的情况下从wordpress数据库中获取标题,描述和附件。我不知道如何同时获取两者,附件和meta_descriptions ......

这是我的查询到目前为止的样子

$query="SELECT post_title, meta_value 
FROM wp_posts p JOIN wp_postmeta pm ON p.ID=pm.post_id 
WHERE post_status='publish' 
AND post_type='page' 
AND post_parent=2330 
AND ( meta_key='_wpseo_edit_description' OR meta_key='_wp_attached_file' ) 
ORDER BY post_date DESC";

此查询返回“doubled”结果,我必须在循环中拆分

______________________________
| post_title | meta_value    |
|------------|---------------|
|  title 1   | 1349          |
|------------|---------------|
|  title 1   | description 1 |
|------------|---------------|
|  title 2   | 1348          |
|------------|---------------|
|  title 2   | description 2 |
|____________|_______________|

1 个答案:

答案 0 :(得分:1)

您需要加入wp_postmeta表格才能获取任何附加信息,例如文件附件或自定义字段。

如何执行此操作的示例:

SELECT p.*, pm2.meta_value AS featured_image, pm3.meta_value AS wpseo_edit_description, pm4.meta_value AS wp_attached_file
FROM `wp_posts` p
LEFT JOIN `wp_postmeta` pm2 ON p.ID = pm2.post_id AND pm2.meta_key = 'wp_attached_file'
LEFT JOIN `wp_postmeta` pm3 ON p.ID = pm3.post_id AND pm3.meta_key = '_wpseo_edit_description'
LEFT JOIN `wp_postmeta` pm4 ON p.ID = pm4.post_id AND pm4.meta_key = '_wp_attached_file'
WHERE p.post_status = 'publish' 
AND p.post_type = 'page' 
AND p.post_parent = 2330 

然后可以使用正常的帖子字段(帖子标题,说明,ID等),以及featured_imagewp_attached_filewpseo_edit_description