你的方法是正确的。如果您发布代码,效果会更好,但类似这样:
$args = array(
\'post_type\' => \'project\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'region\',
\'field\' => \'slug\',
\'terms\' => $region, //set your region
),
),
);
$the_query = new WP_Query( $args );
// The Loop of Projects by Region
if ( $the_query->have_posts() ) {
$projectsid = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
$projectsid[] = get_the_ID();
}
$args = array(
\'post_type\' => \'unit\',
\'meta_query\' => array(
array(
\'key\' => $keyname, //set the key name which store ID
\'value\' => $projectsid,
\'compare\' => \'IN\',
\'type\' => \'NUMERIC\'
),
),
);
$the_query = new WP_Query( $args );
// The Loop of units in project per region
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//do something
}
} else {
//No units for projects in this region
}
} else {
//No projects in this region
}
wp_reset_query();