【问题标题】:Get custom taxonomy terms with a true boolean value of ACF获取具有 ACF 真正布尔值的自定义分类术语
【发布时间】:2017-08-10 23:36:32
【问题描述】:

我有一个名为 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 文档说如何恢复帖子,而不是如何恢复条款: https://www.advancedcustomfields.com/resources/true-false/

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 )
)

那么,第一个问题是,如何恢复每个术语的元字段?

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    我会尝试这个查询来检索给定 $taxonomy 的某个 $term 的帖子,其中 $acf_field_name 设置为 true:

     $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 );
    

    【讨论】:

    • 感谢您的回答。最后我做了另一件事。我会在另一个答案中发布。
    【解决方案2】:

    最后,我使用了另一种方法:获取所有术语,并在循环中输出自定义字段中标记的术语。

    $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; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多