How to Exclude Posts, Pages or Categories From Dynamic Choices

Introduction

Do you have a form that is using the WPForms option of Dynamic Choices? If you’re using Dynamic Choices but you want to exclude specific posts, pages or categories than this is the tutorial for you!

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 that specific post type or taxonomy.

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.

Using dynamic choices for the post type of posts will dynamically generate a list of all the published posts on your site.

For our tutorial, we’re going to display a dropdown of dynamically generated post titles from our site.

Creating the form

To begin, we’re going to create a form that captures the name, email, the ability for the visitor to select which post they liked best and then a comments section as well.

For this reason we’ll add a Name, Email, Dropdown and Paragraph Text form fields to our form.

Once you’ve added the Dropdown field, click the Advanced Options and in the Dynamic Choices dropdown click Post Type. This will bring up another dropdown for Dynamic Post Type Source. When you see this second dropdown, select Posts from the dropdown and click Save on the form.

By default when Dynamic Choices is enabled using the Post Type of Posts, all published posts will be displayed.

Adding the snippet to include posts from a single category

You can even customize these options further by using code snippets. You can only include posts from a particular category or even exclude posts from a particular category.

For instance, in our tutorial, we want to only show posts from a specific category, so our code snippet will be added that will only populate our Dropdown with posts from the category 11.

For help in adding code snippets to your site, please see this tutorial.

/**
 * 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 #4 in form #146, only show entries in category #11
	if ( '146' == $form_id && '4' == $field['id'] ) {
		$args['category'] = '11';
	} 
	return $args;
}
add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_dynamic_choices_categories', 10, 3 );

To find the ID number of your categories, check out this tutorial.

Remember to change your form and field ID in your code snippets to match your form and field ID number. If you need help finding your form and field ID numbers, please review this tutorial.

Limiting multiple forms for multiple category IDs

Did you know that you can also have this code snippet for multiple forms and multiple fields?

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

Limiting 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

You can also exclude specific categories using this 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 );

Now only the posts, pages, tags or categories that you've set in your snippet will populate your dropdown.

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.

Filter Reference: wpforms_dynamic_choice_post_type_args

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

Q: How can I show only the child categories?

A: If you want to include the child categories in your list, you must also include those ID numbers as well as the parent ID number. It’s important to remember that you must have both the parent and the child category IDs listed.

function wpf_dev_dynamic_choices_include( $args, $field, $form_id ) {

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

// Only on form #122 and field #11
if ( $form_id == 122 && $field['id'] == 11 ) {
// Category IDs to include - 87 is parent; 88 and 89 are child categories of this parent
$args['include'] = '87,88,89';
}

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