Wordpress 获取具有真实布尔值ACF的自定义分类术语
我有一个名为Wordpress 获取具有真实布尔值ACF的自定义分类术语,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我有一个名为frontpage的高级自定义字段。是真/假类型 我正在试图恢复所有标记为true的术语。我试过这个: $args = array( 'hide_empty' => 0, 'key' => 'frontpage', 'compare' => '==', 'value' => '1' ); $terms = get_terms( 'people', $args ); if ( ! empty( $terms )
frontpage
的高级自定义字段。是真/假类型
我正在试图恢复所有标记为true的术语。我试过这个:
$args = array(
'hide_empty' => 0,
'key' => 'frontpage',
'compare' => '==',
'value' => '1'
);
$terms = get_terms( 'people', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo $term->name;
}
}
但这将返回所有术语,不管是真是假
我怎样才能得到正确的标记术语?谢谢
编辑:ACF文档说明如何恢复帖子,而不是如何恢复条款:
EDIT2:返回的对象不包含自定义字段,仅包含标准元数据:
Array (
[0] => WP_Term Object (
[term_id] => 2
[name] => Cristina Aiken Soux
[slug] => cristina-aiken-soux
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => personas
[description] => Cras in elementum enim, vitae volutpat sapien. Duis at sem in quam ultrices hendrerit. Class aptent taciti sociosqu ad litora torquent.
[parent] => 0
[count] => 1
[filter] => raw )
)
因此,第一个问题是,如何恢复每个术语的元字段?我将尝试此查询来检索给定$taxonomy中$acf\u field\u name设置为true的某个$term的帖子:
$args = array(
'hide_empty' => 0,
'meta_query' => array(
array(
'key' => $acf_field_name,
'compare' => '==',
'value' => '1'
)
),
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $taxonomy_terms,
),
)
);
$query = get_posts( $args );
最后,我使用了另一种方法:获取所有术语,并在循环中仅输出自定义字段中标记的术语
$args = array(
'hide_empty' => 0
);
$terms = get_terms( $taxonomy, $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$custom_field = get_field('custom_field', $term);
if( $custom_field == '1' ):
echo $term->name;
php endif; ?>
谢谢你的回答。最后我做了另一件事。我将在另一个答案中发布它。