We’ve previously covered how to store form entries in a custom database table when using Advanced Forms Pro and tandem with ACF Custom Database Tables. Storing the entry data is a nice simple solution and works well when you have one form but a far more flexible approach is to have a custom post type for each form and have your form entries create posts instead of entries.
In this post, we’re going to walk through exactly how to approach this. By the end, we’ll have a form that, on submission, creates new posts within a custom post type called
book and all ACF field values on that form will be stored in a custom table called
wp_books. We’re going to use a no-code approach so aside from any UI development work, everything we step through will be achievable from the WordPress admin.
What you need to get started
- Advanced Custom Fields Pro plugin
- Advanced Forms Pro plugin
- ACF Custom Database Tables plugin
- A WordPress powered website
The basic steps necessary to have your forms store entries as custom database table rows
The basic steps are as follows. These are top-level steps which are further detailed below.
- Create a custom post type.
- Create a form.
- Create an ACF field group that is configured to appear on either the custom post type or the form.
- Create a custom database table for the field group.
- Configure the form so that it creates posts of the post type and map any necessary fields.
- Add the form to a WordPress page.
Step 1: Create a custom post type
Whether you use the Custom Post Type UI plugin, WP CLI, or just whip up your own PHP code to create a post type is completely up to you. I tend to use WP CLI but it really doesn’t matter how you create your post type.
To keep this in the spirit of no-code, I suggest checking out https://wordpress.org/plugins/custom-post-type-ui/.
Step 2: Create a form
In your WordPress admin, head to Forms and hit the Add New link. Name your form whatever you like — I’m going with Book Form — and hit publish to save the form. We don’t need to touch anything else here just yet.
Step 3: Create an ACF field group
On the form post edit screen, within the Field meta box, hit the Create field group button. This is what you are looking for:
You’ll be taken to a new field group edit screen. From here, give your field group a name — I’m going with Book Fields — then add whatever fields you need in your form.
Next, we need to set the field group’s location rules so that the field group will appear on either the
book custom post type or our newly created form. Your location rules meta box should look something like the following:
Step 4: Create a custom database table for the ACF field data
Whilst still on the field group edit screen, enable a custom database table for the field group and give it a name. I’m going with the following:
Now you need to save the field group and then run through the database update process. When the field group is updated, you’ll see a large prompt at the top of the screen which gives you an outline of the table that is ready to be applied to your database. It looks something like this:
Hit the blue Manage Database Tables button and follow the prompts to create/update your database table.
At this point, the database table is created and any
book posts you create in the WordPress admin should be saving to the custom database table.
Step 5: Configure the form to create new posts
Return to the edit screen for your newly created form and in the Editing tab of the Form Settings meta box, set the form up to create posts of type
book. I suggest using one of the ACF fields to map to the post title as well but this isn’t necessary. Your form settings should look similar to the following:
Step 6: Add your form to a WordPress post/page
If you return to the display tab of the Form Settings meta box, you’ll notice a shortcode which you can copy and paste into a WordPress post/page. Add this wherever shortcodes are enabled and you’ll now have a form rendering on the front end of your site that, when submitted, stores entry data into a custom database table.
The ability to create table entries like this from ACF powered forms opens up a lot of potential for using WordPress as more of an application foundation. Structured data is much easier to analyse and export and with a bit of strategically-placed SQL, can really speed up a website that uses heavy meta-queries multi-faceted searches.