I created a custom post type with their own categories and wanted to use create a form and have those categories as an option in my form, but when I select the post category field it is only showing categories tied to default Posts and not my custom post type. Is there a way to show my custom post type categories instead? Thanks!
Hey @mareider
I attempted to include the categories from a particular post type, but it did not work as intended. Nonetheless, you can exclude a significant number of categories by their ID. Hence, the below code will exhibit the IDs that are not blocked.
add_filter( 'gform_post_category_args', 'change_categories', 10, 2 );
function change_categories( $args, $field ) {
// add your categories IDs (seperate by comma)
$exclude_categories = '11,32,33';
if ( isset( $args['exclude'] ) && ! empty( $args['exclude'] ) ) {
$args['exclude'] .= ',' . $exclude_categories;
} else {
$args['exclude'] = $exclude_categories;
}
return $args;
}
Alternatively, you can restrict the display of specific categories on the dropdown list by implementing the following code.
add_filter( 'gform_post_category_args', 'allow_specific_categories', 10, 2 );
function allow_specific_categories( $args, $field ) {
// add allowed category IDs
$allowed_categories = array( 12, 34, 56 );
if ( isset( $args['include'] ) && is_array( $args['include'] ) ) {
$args['include'] = array_merge( $args['include'], $allowed_categories );
} else {
$args['include'] = $allowed_categories;
}
return $args;
}
Please add the provided code block in your child theme’s functions.php file or use the Code Snippet plugin.
You can follow the video tutorial here, where I have shared how the provided codes work and when you should use each code block.
GF - Show a specific number of categories
For more details, check the gform_post_category_args
from here:
Best Regards
Faisal A.
My issue isn’t too many categories, but rather the categories for my CPT aren’t showing up at all. I almost need a filter to replace the existing posts categories with my CPT taxonomies. Any ideas on that? Thanks!
First, check if the CPT taxonomy has been registered as a category or with a different name. I used the Books CPT and my taxonomy name was “Category”, which was functioning properly.