2014-09-10 60 views
0

我不得不从wp_query的wordpress数据库中提取标题,描述和附件。我不能让如何在同一时间取两者附件和meta_descriptions ...Wordpress无wp函数的自定义元查询(wo。wp_query())

这是我的查询看起来像到目前为止

$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"; 

该查询返回“翻番”的结果我有循环

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

请阅读[这篇文章](http://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056#271056)我写了关于如何提出正确的问题,它会帮助你得到答案 – 2014-09-10 14:19:11

回答

1

您wlil需要加入到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 

正常后字段(后标题,说明,身份证等)将可用,与featured_imagewp_attached_filewpseo_edit_description沿。

+1

哇,你的问题自从我回答以来已经发生了巨大的变化。不过,我认为我的答案仍然存在。 – Mike 2014-09-10 14:29:39

+0

非常感谢你,这仍然是答案 – Viktor 2014-09-10 14:35:15

+0

必须改变我的问题,因为约翰鲁德尔的提示 – Viktor 2014-09-10 14:36:35