The gform_post_select_post_types filter in Gravity Forms allows you to add custom post types when the Settings API renders a field of type post_select.
Usage
add_filter('gform_post_select_post_types', 'your_function_name');
Parameters
$post_types
(array) – The post types allowed for use in a post_select field. Default value is['post', 'page']
.
More information
See Gravity Forms Docs: gform_post_select_post_types
Examples
Add ‘event’ post type
This example allows the field to return posts of type ‘event’.
add_filter('gform_post_select_post_types', function($post_types) { $post_types[] = 'event'; return $post_types; });
Add multiple post types
This example allows the field to return posts of types ‘event’, ‘portfolio’, and ‘testimonial’.
add_filter('gform_post_select_post_types', function($post_types) { array_push($post_types, 'event', 'portfolio', 'testimonial'); return $post_types; });
Remove ‘page’ post type
This example removes the ‘page’ post type from the allowed post types.
add_filter('gform_post_select_post_types', function($post_types) { $post_types = array_diff($post_types, ['page']); return $post_types; });
Replace default post types
This example replaces the default post types with ‘product’ and ‘service’.
add_filter('gform_post_select_post_types', function($post_types) { $post_types = ['product', 'service']; return $post_types; });
Add post types conditionally
This example adds the ‘event’ post type only if a specific form is being rendered.
add_filter('gform_post_select_post_types', function($post_types) { if (rgar($_GET, 'id') == 'your_form_id') { $post_types[] = 'event'; } return $post_types; });
Note: Remember to replace 'your_form_id'
with the actual form ID.