0

我想选择portata按类别和其他自定义字段过滤的自定义字段“”的不同值。

这是按类别选择自定义值的查询:

global $wpdb;
$querystr2 = "
SELECT DISTINCT wpostmeta.meta_value 
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)


WHERE wpostmeta.meta_key = 'portata'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN($variabile_c)
ORDER BY wpostmeta.meta_value ASC

";

如何将其他自定义字段添加到过滤器?

4

2 回答 2

0

您需要再次针对 $wpdb->postmeta 进行连接,为每个连接使用单独的别名,并在 WHERE 子句中添加几个 AND 以应用额外的条件——例如:

 SELECT DISTINCT wpostmeta1.meta_value
   FROM $wpdb->posts wposts
     LEFT JOIN $wpdb->postmeta wpostmeta1 ON wposts.ID = wpostmeta1.post_id 
     LEFT JOIN $wpdb->postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id 
     LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
     LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
   WHERE wpostmeta1.meta_key = 'portata'
     AND wpostmeta2.meta_key = 'other-custom-field-value'
     AND $wpdb->term_taxonomy.taxonomy = 'category'
     AND $wpdb->term_taxonomy.term_id IN($variabile_c)
   ORDER BY wpostmeta1.meta_value ASC

将“其他自定义字段值”替换为您需要过滤的第二个自定义字段值。

于 2012-10-02T04:04:02.373 回答
0

谢谢亚伦米勒!现在它工作了!

SELECT DISTINCT wpostmeta1.meta_value
FROM $wpdb->posts wposts
     LEFT JOIN $wpdb->postmeta wpostmeta1 ON wposts.ID = wpostmeta1.post_id 
     LEFT JOIN $wpdb->postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id 
     LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
     LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE wpostmeta1.meta_key = 'portata'
     AND wpostmeta2.meta_key = 'alimentazione'
AND wpostmeta2.meta_value = '$variabile_portata'
     AND $wpdb->term_taxonomy.taxonomy = 'category'
     AND $wpdb->term_taxonomy.term_id IN($variabile_c)
ORDER BY wpostmeta1.meta_value ASC

谢谢亚伦米勒。现在,如果您希望通过自定义字段添加 1 个其他文件管理器,我必须添加 1 个左连接?谢谢!

于 2012-10-02T09:16:39.143 回答