Support

Account

Home Forums ACF PRO Output taxonomy terms by a meta field value

Solved

Output taxonomy terms by a meta field value

  • I urgently need a solution for the issue below, hope someone can help me. Thanks in advance.

    I’ve created two custom taxonomies for a custom post type. The taxonomies are Country and City. I’ve added a meta field on the ‘City’ taxonomy creation page and that meta field loads all values from ‘Country’ taxonomy.
    All these done using ACF.
    And I’ve done this to relate the two taxonomies, so for example when creating City terms like ‘New York’, ‘Washington’, ‘Boston’ and ‘Los Angeles’, I’ll choose ‘USA’ from the meta field as the country.

    Now what I want to actually achieve and want help is, output all city terms by each country on a page.

    Like this example that will output on a page:

    USA: New York(10), Washington(14), Boston(74) and Los Angeles(51)
    Canada: Montreal(44), Toronto(31)
    …same for all other countries. (Note that the numbers within parenthesis is the post count)

    I’ve tried looking to the documentation but not found any example that how I can output all the terms related to a field+value.

  • This will only work if your using ACF >= 5.5.0

    The first thing you need is a list of terms country terms using get_terms() https://developer.wordpress.org/reference/functions/get_terms/

    The next step is to loop though the list counties and do another get_terms() with a meta_query value to match the country. The meta_query follows the same syntax as meta_query for WP_Query() https://codex.wordpress.org/Class_Reference/WP_Query

    If you’re not using ACF5 this will be far more complicated.You’ll need to get all of the city terms and then loop through them manually and get the custom field for each one and short them all by country.

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

The topic ‘Output taxonomy terms by a meta field value’ is closed to new replies.