Support

Account

Home Forums General Issues How to use germ_terms() with meta_query for ACF Taxonomy field?

Solved

How to use germ_terms() with meta_query for ACF Taxonomy field?

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

    Through ACF, this also haas 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.

  • 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'.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.