Support

Account

Home Forums Front-end Issues Getting terms by custom field

Solving

Getting terms by custom field

  • According to the WP documentation, it appears to be possible to put a meta_query argument to filter the taxonomy terms retrieved by the function get_terms. However, as seen below, I have tried doing it with the same syntax that I was using on my get_posts statement, and it doesn’t work.

    WordPress seems to recognize this as valid, as it changes the results from giving me all terms to an empty array. But I am certain I have several terms where the ACF display_on_page is checked. Am I doing something wrong?

    $locations = get_terms(array(
    	'taxonomy'			=> 'locations',
    	'meta_query'		=> array(
    		'relation'		=> 'AND',
    		array(
    			'key'			=> 'display_on_page',
    			'value'			=> true,
    			'compare'		=> '='
    		)
    	)
    ));
  • ACF has not yet started to use term meta. See this topic for how to create filters that will store acf fields in term meta so they can be used in term queries. https://support.advancedcustomfields.com/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/

  • Thank you very much! I didn’t manage to get it to work properly, so since I don’t have all that many terms, I just fetched them all and put an if statement to control which ones were displayed. But interesting to know that it’s somehow possible.

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

The topic ‘Getting terms by custom field’ is closed to new replies.