我希望Wordpress查询中有一个foreach。
$aid=0;
foreach ($QA as $key => $value) {
AND wp_postmeta.post_id IN (
SELECT wp_postmeta.post_id
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = '$key'
AND wp_postmeta.meta_value = '$value'
)
$aid++;
}
但它没有那样工作,所以我在寻求帮助 -
如何将foreach放入sql查询?
以下代码的完整示例。
<?php
function query_products($QA) {
global $wpdb;
global $post;
global $pageposts;
$querystr = "
SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'
$aid=0;
foreach ($QA as $key => $value) {
AND wp_postmeta.post_id IN (
SELECT wp_postmeta.post_id
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = '$key'
AND wp_postmeta.meta_value = '$value'
)
$aid++;
}
ORDER BY wp_postmeta.meta_value ASC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
}
$QA = array (
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
);
query_products($QA);
?>
答案 0 :(得分:0)
$querystr = "
SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'
";
$aid=0;
foreach ($QA as $key => $value) {
$querystr .= "
AND wp_postmeta.post_id IN (
SELECT wp_postmeta.post_id
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = '$key'
AND wp_postmeta.meta_value = '$value'";
)
$aid++;
}
$querystr .=" ORDER BY wp_postmeta.meta_value ASC";
答案 1 :(得分:0)
在不知道查询应该完成什么的情况下,我只能建议这样的事情:
<?php
$querystr = "
SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'
AND wp_postmeta.post_id IN (
SELECT wp_postmeta.post_id
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
";
if (count($QA) >= 1) {
$querystr .= " AND ( ";
foreach($QA as $key => $value) {
$querystr .= "(wp_postmeta.meta_key = '{$key}' AND wp_postmeta.meta_value = '{$value}') OR ";
}
// get rid of the last "OR" in the string
$querystr = substr($querystr,0,strrpos($querystr,"OR"));
$querystr .= " ) ";
}
$querystr .= " ORDER BY wp_postmeta.meta_value ASC ";
?>
注意我上面使用了“OR”,因为在使用“AND”子句检查单个字段的多个值时,您永远不会得到任何结果。
答案 2 :(得分:0)
您需要将所有键作为数组传递或迭代地发出完整查询(包括选择)。我不推荐后者,因为性能会很差(过多的数据库往返都很糟糕)。所以,只要$ QA不是太大,我就会这样做。我认为它比使用EXISTS条款最初提交的完整子查询更简单,但这可能更像是一种品味,而不是真正优于另一种。
<?php
function query_products($QA) {
global $wpdb;
global $post;
global $pageposts;
$querystr = "
SELECT wp_posts.*, wp_postmeta_a.*
FROM wp_postmeta AS wp_postmeta_a, wp_posts, wp_postmeta AS wp_postmeta_b
WHERE wp_posts.ID = wp_postmeta_a.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta_a.meta_key = 'country'
AND wp_postmeta_a.meta_value = 'Denmark'
";
$aid=0;
foreach ($QA as $key => $value) {
$querystr .= "
AND EXISTS (SELECT 1
FROM wp_postmeta AS wp_postmeta_b
WHERE wp_posts.ID = wp_postmeta_b.post_id
AND wp_postmeta_b.meta_key = '$key'
AND wp_postmeta_b.meta_value = '$value')
";
$aid++;
}
$querystr .= " ORDER BY wp_postmeta_a.meta_value ASC ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
}
$QA = array (
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
);
query_products($QA);
?>