Support

Account

Home Forums Backend Issues (wp-admin) Adding Taxonomies Dynamically and Removing Unwanted Ones Reply To: Adding Taxonomies Dynamically and Removing Unwanted Ones

  • So here’s where you’re going to have a problem. If the taxonomy does not exist in the options then you’re not calling register_taxonomy() for that taxonomy. If you don’t call register_taxonomy() then the taxonomy does not exist and will not be returned by get_object_taxonomies(), and you won’t be able to delete any of it’s terms because it does not exist.

    I’ve done a search for how to delete terms in WordPress when the taxonomy is deleted and I found nothing.

    Basically, this leaves the all the term information in the DB with no way that I know of to delete it, at least not using WP functions. You’ll need to use get_taxonomies() to get a list of the registered ones and then you’ll need to use $wpdb and do queries to find out what’s in the database that isn’t registered and use db queries to delete it all.

    So this is what I’d do. I would use and acf/update_value filter when the field is saved https://www.advancedcustomfields.com/resources/acfupdate_value/. The value in the database at this point will be the old value and you can compare the old value to the new value and remove that terms for the taxonomies that no longer exist. The problem is that you can’t use ACF functions to get the existing values. Once you have a list of the ones that need to be removed you can use get_terms() and wp_delete_term() https://developer.wordpress.org/reference/functions/get_terms/ wp_delete_term(), https://codex.wordpress.org/Function_Reference/wp_delete_term

    Something like this, this code has not been checked for errors or tested. I’d backup my database before trying it.

    
    add_filter('acf/update_value/name=repeater_field_name', 'my_remove_terms');
    function my_remove_terms($value, $post_id, $field) {
      $repeater = 'filters';
      $sub_field = 'filter_name';
      $sub_field_key = 'field_123456789'; // need this to loop through value
      // get old values
      $count = intval(get_options('options_'.$repeater, 0));
      $current = array();
      for ($i=0; $i<$count; $i++) {
        $option = get_option('options_'.$repeater.'_'.$i.'_'.$sub_field, '');
        if ($value) {
          $current[$option] = $option;
        }
      } // end for
      // end get old values
      if (count($value)) {
        foreach ($value as $row) {
          $option = $row[$sub_field_key];
          if (isset($current[$option])) {
            unset($current[$option]);
          }
        } // end for
      } // end if
      // $current should have a list of the ones that need to be deleted
      if (!count($current)) {
        // nothing needs to be deleted
        return;
      }
      $args = array(
        'hide_empty' => false,
        'fields' => 'ids'
      );
      foreach ($current as $taxonomy) {
        $terms = get_terms($taxonomy, $args);
        if (count($terms)) {
          foreach ($terms as $term_id) {
            wp_delete_term($term_id, $taxonomy);
          } // end foreach term
        } // end if terms
      } // end for each tax to delete
    } // end function