Home Forums Feature Requests Conditional Logic using Taxonomy field


Conditional Logic using Taxonomy field

  • like I said, it’s hard to say… but I wouldn’t mind taking a peek at your code if you would like my help… I have some time for it now…

    I’ll send you my contact info in a PM.

  • This reply has been marked as private.
  • Am I being dumb, can see the PM?

  • My bad I think… You may email me at info [at] berryplasman [dot] com or add me on skype rubbervisions

  • The free version of ACF does NOT return $_POST[‘acf’], hence why it didn’t work in Keith’s case.

    I was not aware of this… and it took some debugging to find out… another good reason to go ACF Pro šŸ˜‰

  • Thanks for your time, very much appreciated, at least we now have an answer and I learnt something

  • Thanks for this, worked for me. Cheers

  • Hi, I seem to be having a bit of a problem. I used Beee’s code and altered it to fit my taxonomies, but unfortunately it’s not working as wanted.

    I have a custom post type with a taxonomy (‘type’), type can either be ‘movie’ or ‘series’.

    My ACF Radio button field has these choices, the value given is the value of the term:

    2 : Movie
    4 : Series

    This will output the value of the field, not the name or array.

    In my functions.php I have:

    function change_media_type( $post_id ) {
        // bail if no ACF data
        if ( empty($_POST['acf']) ) {
        // get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
        $stored_type = wp_get_post_terms($post_id, 'type');
        // get submitted value from acf form
        $posted_type = $_POST['acf']['field_picktype'];
        // get term_id for the submitted value
        $term_id = get_term_by( 'id', $posted_type, 'type' );
        // if stored value is not equal to posted value, then update terms
        if ( $stored_type != $posted_type ) {
            wp_set_object_terms( $post_id, $term_id->term_id, 'type' );
    add_action('acf/save_post', 'change_media_type', 20);

    But it doesn’t seem to change the taxonomy of the post as I want. It changes, but simply changes to “ā€””. Could anyone point me in the right direction?

  • $stored_type should return an array or WP_Error, see this link.
    $posted_type should return the value which was selected in the form.

    With this value you create an a term object/array, see here.

    Now you should have 2 values to compare.

    I think you should look into defining $_POST[‘acf’] differently.
    AFAIK (but I can be wrong), the fields are only defined by key and not by name.

    So I would change
    like I did in my example.

    If you don’t know how to find them go to Custom Fields > Field Groups > Your group > Screen options > Check Field keys.

  • The field key thingy did the trick… can’t believe I missed that after checking almost every other option.

    Thanks a ton, that’ll do!

  • you’re welcome, glad I could help.

  • I ran into this need today and put together a plugin that allows for selection of taxonomy fields and terms in the conditional rules.

    Currently this is written only for ACF v5. I am unsure what it would take to also support v4 and if I plan to do that.

    It should be as simple as install, activate, and start selecting taxonomy fields and terms as needed in your conditional fields.

    Try it out and let me know if you run into any bugs! If it works well for others I will likely release this on the WordPress plugin repository (Unless Elliot wants to roll this functionality into core.

  • @mkeys Thank you! This working fine on Advanced Custom Fields PRO v. 5.5.11

  • That is crazy awesome that you can just whip up a plugin this ACF community has been desperately waiting for.

    Thanks so much.

    Can we leave you donation somewhere? I’d like to think everyone on this post would be happy to throw a little at your skill and generosity.

  • Thank you so much @mkeys. Although I no longer do WordPress development in my daily life, I know the need for this plugin because I get still get the notifications to this thread. šŸ™‚

  • @mkeys Thank you! This working fine on Advanced Custom Fields PRO v. 5.5.11


    Can we leave you donation somewhere? Iā€™d like to think everyone on this post would be happy to throw a little at your skill and generosity.

    That would be very kind. I looked into making a donate button with paypal ( Donate via PayPal form ). It asks for stuff like shipping information which is silly, so if you’d rather just paypal me directly, my paypal email is:

  • I haven’t looked at or tried @mkeys plugin yet, but doesn’t it make you wonder why this hasn’t been implemented, natively, into ACF if Matt Keys was able to write a separate plugin for it? Obviously, this is a massive missing element that would make this feature VASTLY more useful.
    Great job, mkeys! Thanks!

  • @mkeys Thanks so much for this – works perfectly.

  • Just tested this and it doesn’t seem to work within a Group field but when I have flat fields it works.

    Any idea why?

  • Not sure. I haven’t ever used the group field actually, but checking it out on the docs now it looks really handy.

    I am currently chasing down some other bugs with the plugin when multiple conditional fields are set it gets pretty cranky.

    I will take a look at this and see if I can figure out what is going on with group fields too.

    To clarify, is the issue during the field creation while setting the conditional terms. Or is the issue when the fields are being used to enter content while editing a page/post/etc.

  • @mkeys After playing around with it a bit more, I’m not sure if it is Groups or it could be duplicative terms I had with custom taxonomies and ACF fields.

    ACF deals with this well by attaching the key to any field action to avoid namespace collision so you can use the same field name over again (e.g. within repeater fields or groups).

    My guess is WordPress is not liking the way I had things set up.

    For example, if I have a custom post type of Locations with taxonomy slugs of ‘airport’, ‘train-station’, ‘hotel’ then I had a Group named ‘hotel’. With ACF normally this wouldn’t be a problem but I think it is confusing WP.

  • Although this feature request might not be added to ACF for a long time, I would also like to show interest in this functionality.

    I really hope to have this implemented in the following updates. Overall a great plugin, I use it for 90% of the websites I work on, but without this feature, it seems like it’s missing a leg

  • Very long thread so sorry if this has already been suggested… but I was wondering about this too and my first thought was… why not use a Select field instead of a Taxonomy field (where conditional logic is already natively supported) and then use some simple PHP to populate the select field. Like this:

    // Programmically Populate an ACF Select Box
    function acf_load_type_field_choices( $field ) {
        $choices = get_terms( 'battery_type', array('hide_empty' => false) );
        if( is_array( $choices ) ) {
            foreach( $choices as $choice ) {
                $value = $choice->slug;
                $label = $choice->name;
                $field['choices'][$value] = $label;
        return $field;
    add_filter('acf/load_field/name=type', 'acf_load_type_field_choices');

    This is probably not a great solution though because you have the data in two places not… the taxonomy and the select field. Just a thought. Was hoping to think of a clever solution easier than writing a bunch of JS.

  • I’m an idiot. Just up-thread on this SAME page I see that the awesome Matt Keys (I use other plugins by Matt) has created this solution!

  • The OP (or a mod) should solve/close this topic.

Viewing 25 posts - 76 through 100 (of 116 total)

The topic ‘Conditional Logic using Taxonomy field’ is closed to new replies.