Home › Forums › Front-end Issues › 2 custom taxonomies with relationship between them
Hello
I have 2 custom taxonomies – ‘country’ and ‘city’, which represent all countries and cities respectively. Countries are pre-populated on my plugin install, when this custom taxonomy is created. Cities are added later and we connect them to countries via ACF.
The location for field group is: Taxonomy Term = ‘city’.
There’s only one field there: ‘country’, which is ‘Relationship’ – ‘Taxonomy’.
It works just fine in wp-admin – when there’s new city added we can select to which country it belongs and save the data.
Now I’m building a custom front-end registration form where user can select his country (done, no problems) and then the ‘city’ select will should be populated only with cities belonging to this country, not all of them.
I’ve read the docs and tuts and found some useful code to get this:
echo '<p>Cities and their countries</p>';
foreach( $cities as $city ) {
$city_id = 'city_'.$city->term_id;
$country_id = get_field( 'country', $city_id );
$country = get_term($country_id, 'country');
echo $city->name.' ('.$country->name.')<br>';
}
It easily prints a list of cities showing their countries. The most interesting part of the code is the format of $city_id
argument – taxonomyname_taxonomyid
.
Now I tried to reverse it and got this:
echo '<p>Countries with their cities</p>';
echo $country->name.'<br>';
$cities = get_terms('city', array('hide_empty'=>false));
foreach( $cities as $city ) {
$acfcity_id = 'city_'.$city->term_id;
$acfcountry_id = get_field( 'country', $acfcity_id );
$acfcountry = get_term($acfcountry_id, 'country');
if( $acfcountry->name == $country->name )
echo ' - '.$city->name.'<br>';
}
It prints the list of cities only for one country and it works. But it is far from being effective and fast – when the list of cities will be large, this foreach will take some time to loop through. Is there a better way of getting a list of cities
which have particular country
in ACF taxonomy relationship field? Or is this the only way and the only workaround will be to persistently cache those calculated results (a list of cities per one country)?
Hey,
I know this may seem like a bit of a sideways solution, but have you considered using only one taxonomy? For example having a “Locations” taxonomy and then making cities children of their country?
It would make this a ton easier as you can just use the WP query to find posts by parent.
You more than likely have good reason to use multiple taxonomies, sorry if this is not relevant.
Hey @WazzaJB
That was the first thing we tried. We thought it will be a huge list to work with – 195 countries with thousands of cities. And just for the sake of easier to use UI we decided to go with ACF. At first, it sounded cool. But now we’re noticing a significant performance hit with this queries. So, yes. We are going to reconsider using WP taxonomies.
Anyway, ACF has several relational fields and making queries like this might be pretty usual. It would be great to hear from Elliot and see if there’s another, better way of doing this.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.