Using Gravity Forms ‘gform_post_category_args’ PHP action

The gform_post_category_args Gravity Forms PHP filter allows you to modify the $args passed to the WordPress get_terms() function and filter the list of categories displayed when a post category field is configured with the “display all categories” setting selected.

Usage

To apply your function to all forms:

add_filter('gform_post_category_args', 'your_function_name', 10, 2);

To target a specific field, append the field id to the hook name (format: gform_post_category_args_FIELDID):

add_filter('gform_post_category_args_5', 'your_function_name', 10, 2);

Parameters

  • $args (array): The arguments being filtered. See the WP_Term_Query::__construct() codex article for the possible arguments.
    $args = array('hide_empty' => false, 'orderby' => 'name', 'taxonomy' => 'category');
    
  • $field (Field Object): The current post category field.

More information

See Gravity Forms Docs: gform_post_category_args

Examples

Exclude a category from the list

The following example demonstrates how to exclude a category (category with ID=12) from the list of categories:

add_filter('gform_post_category_args', 'change_categories', 10, 2);

function change_categories($args, $field) {
    $args['exclude'] = '12';
    return $args;
}

This code should be placed in the functions.php file of your active theme.