Conditional logic with Taxonomies (ACF)

This post is partially outdated, since ACF has incorporated conditional logic on all fields, but if you want to generically populate the fields, then this might come in handy, since conditional logic doesn’t work then.

If you use the plugin Advanced Custom Fields a lot, like I do you’re bound to run into some sort of limitation.

The problem

My problem (and so it seems, not just mine) was related to taxonomy and conditional logic. While building a site with profiles (with the use of an ACF form), I had a form where profile info had to be entered. For sex I had created a taxonomy, so I added it as a taxonomy field.

And then I ran into this issue. When sex is set to male, there’s no need for info such as cup size and thus I wanted to hide certain fields. And therein lies the problem. With a taxonomy field, there is no conditional logic option. So I had to find another way to solve this.

The solution

The solution was luckily very simple. I created a select field with the options for sex. Make sure the values in the select options correspond with the term ids in your taxonomy !!!

Fortunately there aren’t that many, but in case you have a lot of taxonomy terms you might want to look into dynamically populating select options.


Ok, now we have our taxonomy and a select field, with options which correspond to the taxonomy ids. Now we need to ‘connect’ them. We do this with the function acf/save_post().

This function runs directly before or after a post is saved in the database. In this case we want to hook after a value has been saved. When a post is saved, the value from the newly created select is stored, which is in fact the term id from the taxonomy we want to store.

We then compare the stored value with the submitted value and if they’re different we know we need to update the post terms.

Working code

  1. function change_profile_sex( $post_id ) {
  3.     // bail if no ACF data
  4.     if ( empty( $_POST['acf'] ) ) {
  5.         return;
  6.     }
  8.     $posted_sex = false;
  9.     $term       = false;
  10.     $stored_sex = wp_get_post_terms( $post_id, 'taxonomy' );
  12.     if ( isset( $_POST[ 'acf' ][ 'field_57e3ed6c92fd1' ] ) ) {
  13.         $posted_sex = $_POST[ 'acf' ][ 'field_57e3ed6c92fd1' ];
  14.         $term       = get_term_by( 'id', $posted_sex, 'taxonomy' );
  15.     }
  17.     if ( $term && false == $stored_sex ) {
  18.         wp_set_object_terms( $post_id, $term->term_id, 'taxonomy' );
  19.     } elseif ( $term && $stored_sex ) {
  20.         if ( $stored_sex[0] != $posted_sex ) {
  21.             wp_set_object_terms( $post_id, $term->term_id, 'taxonomy' );
  22.         }
  23.     }
  24. }
  25. add_action( 'acf/save_post', 'change_profile_sex', 20 );


  • This example is set to change 1 value only. If you want to change multiple taxonomies, the function needs to be adapted.
  • Please test this on a local/test environment first before putting it live.

Read the entire forum topic here.

Update: there’s now also a plugin which does the trick.