Add New Create Post Action Taxonomy

The Question

Is there a way to create a new taxonomy for a custom post type from the value of a text field when using Pro's Create Post form action?

Formidable Slack Community Member

The Answer

Yes! In fact, Formidable has a knowledge base example on how to Create a new WordPress Category. The example provides basic code for creating a new WordPress Category, but it doesn't go far enough to demonstrate how to assign the new taxonomy to a custom post type created by Pro's Create Post form action.


The original poster of this question designed her form to include a text field where an end user could enter a city. Once the form is saved, a custom post type is created. Her goal is to create a new taxonomy from the entered city and have the newly created post automatically assigned to that taxonomy to aid in categorizing her searches.

The Caveat

Creating the new taxonomy from a text field can be tricky and prone to errors. Why? Because when people are left to their own accord to enter free-form text, mistakes happen like spelling and capitalization errors. It's really hard to control proper data entry when free-form typing is the order of the day.

Arguably, there are better alternatives. Nowadays, if I'm collecting address information on a Formidable Form, I recommend integrating it with the Google Maps Places API. We've enjoyed a lot of success with our clients by integrating this API with Formidable. It provides an auto complete function to help a user as they start typing an address. Once the correctly formatted address is received from the API, you can populate any form field with the returned content. It guarantees data entry consistency. (Maybe we'll have to develop a tutorial about this.)

Nevertheless, this article is about using a text field to add a new city taxonomy to a custom post type. If you read our Tips & Tricks article, Understanding Pro’s Create Post Action Taxonomies, we'll be using the same Companies custom post type for this tutorial. That particular CPT has a defined City taxonomy. In the previous article, this taxonomy was populated with four cities: Atlanta, Nashville, Raleigh, and San Francisco. That will be our starting point.

The Process

This process hooks into Formidable's frm_after_create_entry action. As shown in the knowledge base snippet, this is the best place to execute our function because it is also the only Formidable action hook that can provide us with access to the ID of the post created by the Create Post form action. Why do we need the post id?

Structurally, the created post's id is stored in the post_id field in the wp_frm_items table. The post_id field can't be populated until after the post is created. This means the post is created before the form entry details are written to Formidable's tables. Therefore, we can only retrieve the information we need from the entry after it has been created.

We need the post ID because the WordPress function we have to use to assign the new taxonomy to the post requires it. Please reference wp_set_object_terms for further details.

The Code

The code is straight forward and much simpler than one may expect.

This function retrieves the taxonomy term from the text field via the PHP $_POST associative array. It also retrieves the form entry object via the FrmEntry::getOne() function. After we have our variables, we check for the post_id. If there is no post_id, we exit the function. If there is a post_id, wp_set_object_terms takes care of the rest. That's all there is to it.

Let's keep something in mind. As mentioned earlier, because the city is being added through a text field, it's conceivable that a term may be entered into the database more than once due to data entry errors on the front end, but if the data stays clean, there shouldn't be any problems.

The Result

Here's the result of our hard work. Remember earlier, we said the custom post type we used to develop this code has a defined taxonomy that was populated with four cities: Atlanta, Nashville, Raleigh, and San Francisco. If you look at the terms list below, you'll see we've added the cities of Durham, Florence, and Jersey City. Each of these new cities is associated with one post. These were created and applied to the CPT entries programmatically with the function above.

Add New Create Post Action Taxonomy result screen capture

Other Code Example

In response to Michael's inquiry below, this example shows how this process can be used on multiple forms:

The switch statement tests for the form_id. If the form_id is one of your target forms, then assign the variables and process the taxonomy. As a minimalist coder, I believe that a function should do only one thing.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *


  1. Much more clear explanation than in the KB. Question: I have a few different taxonomies I need to create from different forms. In order to do this do I simply ensure unique function names for each one? If so, I’d *love* to make one function to handle all the different forms.

    Care to share insights and code that would accomplish this?

    • Sure! You can have as many forms as you want in a function like this. You can either create if statements for each form, which would result in a lot of code, or if you’re a code minimalist like me, create a second function to process the taxonomy after you assign the variables in the first function. In fact, I added a code example to show you how I would manage multiple forms. As an FYI, I haven’t tested the multi-form code, so if you have any problems running it, let me know so I can fix it.

  2. This has been a super useful example. Is there one that also maps a field to the taxonomy term’s description field?