Search code examples
wordpresslogictaxonomycustom-taxonomy

Conditional logic with WordPress taxonomy in wp-admin area


Is it possible to add conditional logic to taxonomy in admin post area (I mean while creating new post)? Like I have two taxonomies

  1. Destinations with terms (a, b, c, d) and
  2. services with terms (1, 2, 3, 4 etc)

How to show services taxonomy in post only if Destination is a or c?


Solution

  • It's basic jQuery, you'll just need to study the DOM and define the relevant IDs or write a selector that matches your taxonomies.


    add_action( 'admin_head-post-new.php', function() {
        ?>
        <script>
        jQuery(document).ready(function($){
            $('#serviceschecklist li label input:checkbox').attr("disabled", true);
            $('#destinationchecklist li label input:checkbox').change(function() { 
                $checked = $(this).is(':checked');
                $val = $(this).val();
                switch( $val ) {
                    case '8':
                        if( $checked ) {
                            $('#in-services-10').attr('checked',true);
                            $('#in-services-12').attr('checked',true);
                        } else {
                            $('#in-services-10').attr('checked',false);
                            $('#in-services-12').attr('checked',false);
                        }
                    break;
                    case '9':
                        if( $checked ) {
                            $('#in-services-11').attr('checked',true);
                            $('#in-services-13').attr('checked',true);
                        } else {
                            $('#in-services-11').attr('checked',false);
                            $('#in-services-13').attr('checked',false);
                        }
                    break;
                }
            });
        });
        </script>
        <?php
    });