Description

The wpforms_dynamic_choice_post_type_args filter is used to populate the Dynamic Choices for post types and taxonomies.

Parameters

$args
(array) (Required) Arguments from the post submission such as Category ID, Tag ID, Author ID, etc.
$field
(array) Sanitized entry field values/properties.
$form_id
(int) Form ID.

Source

wpforms/includes/functions/form-fields.php

More Information

The wpforms_dynamic_choice_post_type_args filter provides a list of supported arguments from the post such as Category. Similarly, you can also use the wpforms_dynamic_choice_taxonomy_args to pull arguments from the taxonomy of Tags.

In this example shown below, the function will process on form ID 789 for field ID 10 and only include posts from the category 11.

Examples

/**
 * Limit posts or pages displayed by category.
 *
 * @link https://wpforms.com/developers/wpforms_dynamic_choice_post_type_args
 *
 * @param array $args       Arguments from post submission.     
 * @param array $field      Sanitized field data. 
 * @param int   $form_id    Form ID
 *
 * @return $args
 */

function wpf_dev_dynamic_choices_categories( $args, $field, $form_id ) {
    
     // Only on form #789 and field #10
     if ( $form_data[ 'id' ] == 789 && $field[ 'id' ] == 10 ) {

         $args[ 'category' ] = '11';		 
    
     }
	
     return $args;
}

add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_dev_dynamic_choices_categories', 10, 3 );

Reference Articles

How to Exclude Posts, Pages or Categories From Dynamic Choices

FAQ

Q: How can I show only the child categories?

A: To display child categories in your form, you need to include both the parent category ID and the child category IDs. This is because the current functionality requires that the parent category is specified to correctly fetch and display its child categories.

/**
 * Limit posts or pages displayed by category.
 *
 * @link https://wpforms.com/developers/wpforms_dynamic_choice_post_type_args
 *
 * @param array $args   Arguments for post submission.     
 * @param array $field  Sanitized field data. 
 * @param int   $form_id Form ID
 *
 * @return array Updated arguments
 */

function wpf_dev_dynamic_choices_include( $args, $field, $form_id ) {
    // Ensure form ID is correctly handled
    if ( is_array( $form_id ) ) {
        $form_id = $form_id[ 'id' ];
    }

    // Apply only to specific form and field IDs
    if ( $form_id == 122 && $field[ 'id' ] == 11 ) {
        // Include category IDs - 87 is the parent; 88 and 89 are child categories
        $args['include'] = '87,88,89';
    }

    return $args;
}
add_filter( 'wpforms_dynamic_choice_taxonomy_args', 'wpf_dev_dynamic_choices_include', 10, 3 );

This snippet ensures that both the parent category (ID 87) and its child categories (IDs 88 and 89) are included in the dynamic choices.