我想选择按类别和其他自定义字段过滤的自定义字段“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
";
如何在过滤器上添加其他自定义字段?
答案 0 :(得分: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
将'other-custom-field-value'替换为您需要过滤的第二个自定义字段值。
答案 1 :(得分:0)
谢谢Aaron Miller!现在它有效!
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
谢谢Aaron Miller。 现在,如果你想通过自定义字段获得另外一个文件管理器,我必须添加1个左连接? 谢谢!