0

I have a custom taxonomy, Events (“event”).

Through ACF, this also has a Taxonomy field, “Topic”, linking it to a secondary taxonomy, Topic (“topic”).

I want to get_terms() for all Events terms which bear the specific “Topic” term “Media”, which happens to have term ID 422.

I know get_terms() can take meta_query, but I don’t understand how to properly get what I need.

Both of these return no results…

'meta_query' => array(
        array(
          'key'     => 'Topic',
          'value'   => '422',
        )
      )

'meta_query' => array(
        array(
          'key'     => 'Topic',
          'value'   => 422,
        )
      )

FYI, my “Topic” Taxonomy ACF field “Return Value” is set to “Term ID”, which is why I attempted passing ‘422’.

As it happens, to test, when I…

– do a get_terms() without meta_query

– then get_term_meta() to get the “Topic” of the terms

– then print out the result…

… I see a formulation like Array ( [0] => Array ( [0] => 422 ) )

That’s where the 422 is buried.

2 Answers 2

0

Answer - the correct formulation is...

'meta_query' => array( array( 'key' => 'Topic', 'value' => "12057", 'compare' => 'LIKE' ) )

In full:

` $args = array( 'taxonomy' => 'event', // 'order' => 'ASC', 'hide_empty' => false, 'hierarchical' => true, // 'parent' => 0,

  'meta_query' => array(
    array(
      'key'     => 'Topic',
      'value'   => "12057",
      'compare' => 'LIKE'
    )
  )


);

$events = get_terms( $args );`

Specifically, it seems 'compare' => 'LIKE' needs to be present.

Initially, I had thought the solution was to use double quotes on the value, which I inferred from @hube2's response to another question.

But actually it works with single or double. The clincher is the present of 'compare' => 'LIKE'.

0

'compare' => 'IN' Since you're comparing against a single value, you can use IN as the comparison operator.

$args = array(
    'taxonomy' => 'event',
    'meta_query' => array(
        array(
            'key' => 'topic', // Meta key for ACF Taxonomy field
            'value' => 422, // Term ID to match
            'compare' => 'IN', // Match any of the provided values
        ),
    ),
);

$terms = get_terms( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.