Support

Account

Home Forums General Issues Automatically register a field group when adding a category

Helping

Automatically register a field group when adding a category

  • Whenever I add a new post category to my admin, I would like to automatically register a new field group with the same name as the category. I’m thinking I’d need to use the register_field_group function for this somehow?

    Also, whenever I rename a category, I would also like to rename the matching field group so they have the same name. Can this be done?

  • This is an old topic and you’ve probably moved on but for anyone that’s looking for the same type of thing.

    The first thing I ask is what will having a different field group for each category give that that having the same field group on all categories won’t.

    If you still want to do this, you would not create a new field group every time a new category is created, you would create some custom php code that would run and create a field group and fields for each category that exists.

    I’d start by exporting a field group that’s attached to a specific category first and use this for an example. Then I would add an function that runs on WP init

    
    add_action('init', 'add_category_field_groups');
    function add_category_field_groups() {
      // use get_terms to get a list of categories
      // https://developer.wordpress.org/reference/functions/get_terms/
      // loop through the categories and create a field group
      // I would set the group key to something like 
      // "group_termid000{$term->term_id}"
      // see https://www.advancedcustomfields.com/resources/register-fields-via-php/
    }
    

    Hope that helps someone

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

The topic ‘Automatically register a field group when adding a category’ is closed to new replies.