aitor
aitor

Reputation: 2735

Get custom taxonomy terms with a true boolean value of ACF

I have an Advanced Custom Field called frontpage. Is true/false type.

I'm trying to recover all terms marked as true. I tried this:

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

But this returns all terms, regardless true or false.

How can I get just true marked terms? Thanks!

EDIT: ACF docs says how to recover post, not how to recover terms: https://www.advancedcustomfields.com/resources/true-false/

EDIT2: The object returned does not contain custom fields, just standard meta data:

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

So, the first question is, how can I recover the meta fields of each term?

Upvotes: 1

Views: 2697

Answers (2)

aitor
aitor

Reputation: 2735

Finally, I've used another approach: To get all terms and, in the loop, ouput just terms marked in the custom field.

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

Upvotes: 0

fabio
fabio

Reputation: 624

I would try this query to retrieve posts of a certain $term for a given $taxonomy that have $acf_field_name set to 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 );

Upvotes: 4

Related Questions