How to Exclude Posts, Pages or Categories From Dynamic Choices

Overview

Would you like to exclude certain posts, pages or categories from the Dynamic Choices setting in WPForms? When using this option, you may not want every post to populate the choices from a certain category. In this tutorial, we’ll show you how you can exclude items from the Dynamic Choices.

The Multiple Choices, Checkboxes, and Dropdown fields can be configured to dynamically display post or page titles as Dynamic Choices.

By default when Dynamic Choices is enabled, all published items of the selected source will be displayed.

For example, when Dynamic Choices is set to Post Type, and the Dynamic Post Type Source is set to Posts, all published posts will be displayed as field choices.

By default when Dynamic Choices is enabled, all published items of the selected source will be displayed.

Implementation Options

If you don’t want every post or page to be included in these options, it’s possible to use some custom code to limit those posts or pages by specific categories by adding either of these code snippets to your site.

Remember to change your form and field ID in the code snippets below to match your form and field ID number as well as update the category ID number to match the category you want to use.

Limit by Category

The code below limits dynamic options within two different forms:

  • Field ID 1 of form ID 363 to display only posts with the category ID 11
  • Field ID 20 of form ID 225 to display only posts from category ID 5
/**
 * Limit posts or pages displayed by category.
 *
 * @link https://wpforms.com/developers/how-to-exclude-posts-pages-or-categories-from-dynamic-choices/
 *
 */
function wpf_dynamic_choices_categories( $args, $field, $form_id ) {
	// For field #1 in form #363, only show entries in category #11
	if ( '363' == $form_id && '1' == $field['id'] ) {
		$args['category'] = '11';
	// For field #20 in form #225, only show entries in category #5
	} elseif ( '225' == $form_id && '20' == $field['id'] ) {
		$args['category'] = '5';
	}
	return $args;
}
add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_dynamic_choices_categories', 10, 3 );

Limit by Taxonomy

A taxonomy is a term used in WordPress to define things like Categories and Tags.

If the Dynamic Choices is set to Taxonomy, and the Dynamic Taxonomy Source set to Categories, all categories will be used for the field choices.

It is also possible to exclude categories when showing all taxonomies as choices with the code snippet below

To exclude specific categories, we can use the following snippet.

/**
 * Exclude categories from dynamic choices.
 *
 * @link https://wpforms.com/developers/how-to-exclude-posts-pages-or-categories-from-dynamic-choices/
 *
 */
function wpf_dev_dynamic_choices_exclude( $args, $field, $form_id ) {

    if ( is_array( $form_id ) ) {
        $form_id = $form_id['id'];
    }

    // Only on form #212 and field #16
    if ( $form_id == 212 && $field['id'] == 16 ) {
        // Category IDs to exclude
        $args['exclude'] = '4,5,6,11';
    }

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

And that’s it! Would you like to know how to add field values for these same form fields? Take a look at our article on How to Add Field Values for Dropdown, Checkboxes, and Multiple Choice Fields.

FAQ

Q: Can I only show posts from a particular author id?

A: Absolutely! You can just tweak the code snippet to what is shown below and only posts from that specific author ID will show up in your choices.

Just remember to update the 526 to match your own form ID and update the $args['author'] = '9'; to match the user ID. If you need help identifying the author ID, please see this tutorial from our friends at WPBeginner.

function wpf_dynamic_choices_author( $args, $field, $form_id ) {
    if ( '526' == $form_id ) {
        $args['author'] = '9';
    }
    return $args;
}
add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_dynamic_choices_author', 10, 3 );