您可以使用snippet I found 查询附加到特定职位类型的术语:
function wpse_207655_terms_by_post_type_or_meta( $clauses, $taxonomy, $args ) {
$meta_query = new WP_Meta_Query;
$meta_query->parse_query_vars( $args );
$post_types = \'\';
if ( ! empty( $args[\'post_type\'] ) ) {
if ( $_post_types = array_intersect( ( array ) $args[\'post_type\'], get_post_types() ) )
$post_types = implode( "\',\'", $_post_types );
}
if ( $post_types || $meta_query->queries ) {
global $wpdb;
$clauses[\'join\'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
$clauses[\'orderby\'] = "GROUP BY t.term_id {$clauses[\'orderby\']}";
if ( $post_types )
$clauses[\'where\'] .= " AND p.post_type IN (\'$post_types\')";
if ( $meta_query->queries ) {
$sql = $meta_query->get_sql( \'post\', \'p\', \'ID\' );
$clauses[\'join\'] .= $sql[\'join\'];
$clauses[\'where\'] .= $sql[\'where\'];
}
}
return $clauses;
}
add_filter( \'terms_clauses\', \'wpse_207655_terms_by_post_type_or_meta\', 10, 3 );
现在您可以使用
post_type
中的参数
wp_dropdown_categories
(这实际上只是传递给
get_terms
, 这就是我们的过滤器发挥作用的地方):
wp_dropdown_categories( \'taxonomy=location&post_type=X\' );
Update: 现在您还可以使用
WP_Query
元参数:
wp_dropdown_categories( \'meta_key=foobar\' );
。。。或完整的元查询:
wp_dropdown_categories( array(
\'post_type\' => \'foobar\',
\'meta_query\' => array(
array(
\'key\' => \'this\',
\'value\' => \'that\',
),
array(
\'key\' => \'foobar\',
),
),
));