以下查询可以获取自定义字段键的所有值。收集自:http: //goo.gl/94qw3
function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
global $wpdb;
if( empty( $key ) )
return;
$r = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $r;
}
$my_var = get_meta_values( 'YOURKEY' );
当匹配“自定义字段 A”键的值“2010”时,我想获取“自定义字段 B”键的所有值。
例子:
我有 2 个键(2 个自定义字段)
-------------------------
Movie Yr | Artist
---------------------------
2000 | Artist One
2008 | Artist Two
2012 | Artist Three
| Artist Four
| Artist Five
---------------------------
通过上述查询,我可以获得以下列表。
---------
Movie Yr
---------
2000
2008
2012
---------
现在,在我选择字段“Movie Yr”的值“2010”后,我想获取艺术家列表。不是所有的艺术家,只有那些与“2010”有关的艺术家。就像是
---------
Artist
---------
Artist Two
Artist Four
Artist Five
---------