很抱歉提出了一个旧帖子,但我在为我的用例寻找答案时遇到了这个问题。
我想检索一个post类型的所有可用分类法,还想检索每个分类法的所有可用术语。
感谢Nick B为我提供了正确的答案:https://wordpress.stackexchange.com/a/357448/198353
// get a list of available taxonomies for a post type
$taxonomies = get_taxonomies([\'object_type\' => [\'your_post_type\']]);
$taxonomyTerms = [];
// loop over your taxonomies
foreach ($taxonomies as $taxonomy)
{
// retrieve all available terms, including those not yet used
$terms = get_terms([\'taxonomy\' => $taxonomy, \'hide_empty\' => false]);
// make sure $terms is an array, as it can be an int (count) or a WP_Error
$hasTerms = is_array($terms) && $terms;
if($hasTerms)
{
$taxonomyTerms[$taxonomy] = $terms;
}
}