This article looks at how to create a custom post type for “exhibition”.
What is a “custom post type”?
When you first install WordPress, two post types are used. These are posts and pages. You see these in the left hand panel of your WordPress dashboard.
As you will probably know, there are some differences between posts and pages which show themselves when you go in to edit a post or a page. So for instance, posts can have tags and categories whereas pages have a hierarchy (a “parent”). At its simplest, you see these differences as different fields in the edit pages.
For a while now, WordPress has allowed you to create a custom post type. This is as it sounds – you can create a new type of content (in our case for this article, an exhibition post type) which can then be made to appear in the dashboard as well.
As well as post types, WordPress supports custom fields. This means that for any post you can add additional fields.
Out of the box, the way WordPress handles these custom fields is a bit basic – once you create a custom field it appears everywhere and is just of a simple text type. We’re going to use a plugin called Advanced Custom Fields to greatly enhance this in-built functionality.
Step 1: create the exhibition custom post type
We’re going to do all our editing in our newly-created child theme for reasons I outlined earlier.
Open your child theme folder, and create a new file called functions.php.
In here, paste this code:
Save it, and then go to your WordPress dashboard and refresh. You should see this:
So far so good – you’ve added a custom post type called “exhibition”!
Step 2: install Advanced Custom Fields
ACF is one of our favourite plugins – it allows you to add a really nice, customisable, user-friendly interface over the top of the existing WordPress custom field functionality.
Go to Plugins > Add New and then search for “advanced custom fields”. Install and then activate the plugin.
Step 3: add fields to Exhibition custom post type
Once installed, you’ll see a new “Custom Fields” option in the dashboard. Click here, then choose to add a new Field Group. Call the field group “Exhibition”.
Let’s add just one new field for now – we will expand on this later. Click to add field, call it “Exhibition Start Date” and give it a Field Type of Date Picker. Finally, in the panel called “Location”, change the drop down so it shows [Post Type] [is equal to ] [exhibition]. This tells WordPress to only display our new field when someone is editing a post of type “exhibition”:
Save / Publish and then exit the Custom Fields editor.
Step 4: test it
Go into Exhibitions > Add new. You should see that as well as the normal fields (title, rich text editor etc) there is an additional one, called “Exhibition Start Date” – and that if you click into it, you get a nice calendar picker. This is because we chose date picker as the field type in the step above:
Enter a date, then save your post. If you now exit the post and go back in you will see that your date is saved…!
Try adding some new fields to the Exhibition ACF field group – and also try fiddling with some of the display options – then hop back into an exhibition post type to see what changes.
Next time we’ll look at how to display your data on an exhibition page.