gform_post_category_args

Description

The gform_post_category_args filter allows developers to change 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

Applies 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

ParameterTypeDescription
$argsarrayThe args being filtered. See the WP_Term_Query::__construct() codex article for the possible arguments.
$fieldField ObjectThe current post category field.

$args defaults to the following:

$args = array( 'hide_empty' => false, 'orderby' => 'name', 'taxonomy' => 'category' );

Examples

Exclude a category from the list of categories.

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;
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in GFCommon::add_categories_as_choices() in common.php.