Support

Account

Home Forums General Issues Taxonomy field option Save Terms prevents the default category from saving

Solved

Taxonomy field option Save Terms prevents the default category from saving

  • On our post pages we have two acf taxonomy fields. One points to a custom taxonomy, while the other points to the standard category taxonomy. We show these two, they are in the same field group, and hide the default category field.

    The trouble is with the field pointing to the standard category taxonomy. For some reason, when the ‘Save Terms’ option is set to ‘Yes’, the default category selected in Settings > Writing > Default Post Category isn’t saved on the post. If I set this option to ‘No’, it works as expected.

    I’ve reproduced this on another site as well.
    What I did to reproduce:
    – Create a ACF taxonomy field
    – Create a new post (see the default category is applied)
    – Set ‘Save Terms’ to ‘Yes’ on the ACF taxonomy field
    – Create a new post (the default category is not applied)

    We have ACF PRO version 5.4.8.

    Can anyone help with this?

  • The problem is that ACF uses wp_set_object_terms() to update the terms. This happens after WP has already saved the post. ACF does not look to see if the post already has terms set for a taxonomy before doing the update. You might consider submitting a new support ticket, this could be considered a bug, but the developer might not https://support.advancedcustomfields.com/new-ticket/.

    But the default value is generally only used if no category is selected, or do you mean that this is the case?

    Using filters and hooks available in ACF it would be possible to work around this issue. You can use an acf/update_value filter https://www.advancedcustomfields.com/resources/acfupdate_value/ to add the default term to the value array before it is updated

    
    add_filter('acf/update_value/name=field_name', 'set_default_category', 9, 3);
    function set_default_category($value, $post_id, $field) {
      if (!is_array($value) || !count($value) {
        $value = array(1); // 1 = Uncategorized
      }
      return $value;
    }
    
  • Thanks, John! I’ll probably work around it by using the filter in your example.

    Yeah, the desired behavior is that when the customer doesn’t select a category, the default is applied. I noticed it didn’t happen, and I need it to happen because we hide posts based on categories on the site in the pre_get_posts action.

    Thanks!

  • Hey, John!

    I’m trying to do this:

    add_filter('acf/update_value/name=categories', 'set_default_category', 9, 3);
    function set_default_category($value, $post_id, $field) {
      if (!is_array($value) || !count($value)) {
        $value = array(get_option('default_category', 1));
      }
    
      return $value;
    }

    I know it triggers, and I know $value has the id of the selected default category.
    Yet, when the the post is saved, the category doesn’t show up.
    Any tips?

  • The only thing I can think of is to change the priority of the filter to something > 10. Maybe it needs to run after ACF’s internal filter.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Taxonomy field option Save Terms prevents the default category from saving’ is closed to new replies.