Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 组合2个SQL查询以避免foreach循环_Wordpress_Mysql - Fatal编程技术网

Wordpress 组合2个SQL查询以避免foreach循环

Wordpress 组合2个SQL查询以避免foreach循环,wordpress,mysql,Wordpress,Mysql,我想查询一个数据库,以便获得所有想要的帖子以及它们的图片。我通过两个不同的查询成功地完成了这项工作。第一个将所有帖子与所需字段放在一起,第二个在foreach循环中运行,以获取每个帖子的图像。我的问题是这需要很长时间,我想避免id。我需要一个解决方案,以避免第二个查询中的foreach循环,并将其合并到第一个查询中 查询1 "SELECT * FROM wp_posts"; 和foreach返回posts.ID 查询2 "SELECT wp_posts.guid IN (Select wp_p

我想查询一个数据库,以便获得所有想要的帖子以及它们的图片。我通过两个不同的查询成功地完成了这项工作。第一个将所有帖子与所需字段放在一起,第二个在foreach循环中运行,以获取每个帖子的图像。我的问题是这需要很长时间,我想避免id。我需要一个解决方案,以避免第二个查询中的
foreach
循环,并将其合并到第一个查询中

查询1

"SELECT * FROM wp_posts";
和foreach返回posts.ID

查询2

"SELECT wp_posts.guid IN (Select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.meta_key = '_thumbnail_id' AND wp_postmeta.post_id = ('the returned post id foreach of ID output of the first query')"

我知道在posgresql中,一个JOIN就可以做到这一点。我对word press了解不够,但我会调查一下并更新这个答案

更新此选项可以做到:

SELECT guid FROM wp_posts 
OUTER JOIN wp_postmeta on wp_postmeta.post_id = wp_posts.id
where wp_postmeta.meta_key = '_thumbnail_id';

联接将两个表组合在一个特定键上。

使用规范化方法,它将通过避免两个查询来解决您的问题:

SELECT WP.guid
FROM wp_posts WP,
     wp_postmeta WPM
WHERE wp_postmeta.meta_key = '_thumbnail_id'
  AND WPM.post_id=WP.yourForeignField
GROUP BY WP.guid

您可以像这样使用wordpress默认函数
get_posts()

$args = array('posts_per_page'=> -1,'meta_key'=> '_thumbnail_id' );
get_posts( $args );