我正在尝试获取已分配给特定分类法术语的高级自定义字段组。我遇到了:
acf_get_field_groups();
从帖子ID和分类法中轻松地获得一个群组很容易,但是当我想让它成为一个学期时,我就没有运气了。
尝试了这些:
acf_get_field_groups(array('term_id' => '1293'));
acf_get_field_groups(array('taxonomy' => 'services','term_id' => '1293'));
该函数不能执行我期望的功能,或者我没有正确提供参数格式。
您尝试的逻辑实质上是“返回所有显示在具有term_id xyz的所有帖子上的所有字段组”。看起来像这样:
$posts = get_posts(array(
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => 1, /// where term_id of term 1 is "1".
'include_children' => false
)
)
));
// Get the id's from every post.
$post_ids = wp_list_pluck($posts, 'ID');
// Combine them into a comma split string
$post_ids = implode(',', $post_ids);
// Fetch our field groups.
$field_groups = acf_get_field_groups(array('post_id' => $post_ids))
您还可以创建一个自定义ACF过滤器来为您匹配它,这可能是最好,最有效的方法:
add_filter('acf/location/rule_match/post_taxonomy',
function ( $result, $rule, $screen ) {
$value = acf_maybe_get( $screen, 'post_taxonomy' );
// bail early if not taxonomy
if( !$value ) return false;
$match = ( $value == $rule['value'] );
// Return.
return $match;
}
, 10, 4);
var_dump(acf_get_field_groups(array('post_taxonomy' => 'post_tag:test')));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句