WordPress has significantly evolved from its roots as a basic blogging platform. With the introduction of Custom Post Types (CPTs), it has established itself as a powerful and versatile Content Management System (CMS) that can handle any type of content.
However, when it comes to creating new CPTs, the default options—essentially just the title and editor box—are inadequate.
This lack of comprehensive features falls short for users who need more than what standard Post or Page types offer.
While WordPress does provide a “Custom Fields” meta box, relying on this method is inefficient; it requires users to repeatedly define fields for each new post.
We must streamline this process for our clients and ourselves. If a CPT needs to maintain consistent fields across posts—something that is frequently necessary—then we need a more robust solution to add those fields effectively.
Creating a Custom Post-Type
There are lots of outstanding plugins, however, I strongly believe these plugins introduce unnecessary complexity to what is essentially a straightforward process.
For any new project, I go straight to writing the code. I advocate for creating a dedicated plugin for the project, aptly titled “Project CPT,” where all custom post types, custom taxonomies, and related functionalities can be organized efficiently.
Placing this code in the theme’s functions.php file is not advisable. While it’s an option, it muddles the distinction between data and presentation. Clients will likely redesign their site multiple times, but they will want to keep the custom post types they have established.
Since all functions in functions.php can be developed into standalone plugins, I firmly maintain that these elements should remain separate. Take our project “The Bookworm Blog,” for which we need a “Book” custom post type. Here’s the essential code for that:
We need to expand beyond the standard fields that WordPress provides. While I’ve enabled the “Custom Fields” feature for demonstration, the native custom fields are not user-friendly for our client. We must utilize a third-party plugin to enhance usability and meet our requirements effectively.
How To Download, Install and Activate Advanced Custom Fields
Advanced Custom Fields is a powerful plugin that offers an intuitive visual interface for creating custom fields. You can easily create field groups and assign them to your custom post types or a variety of other criteria.
While it is available for free in the WordPress Plugin Directory, investing in premium features like Repeater Fields and Flexible Content Fields will significantly enhance your capabilities. Don’t miss out on these invaluable tools.
Advanced Custom Fields, found in the WordPress Plugin Directory, provides a more powerful interface for creating custom fields compared to WordPress native custom fields functionality.
Creating Field Group
ACF delivers a robust array of field types that you must utilize for your custom post type (CPT), including simple text fields, a WYSIWYG editor, image uploads, file uploads, and more.
The post title will decisively represent the book title, while the main editor box will showcase the book synopsis. Therefore, we need to establish the following fields:
- Author: Text, with no HTML formatting
- Publisher: Text, with no HTML formatting
- Copyright date: Numeric (not a “date,” since we only want to list the year)
- Cover: Image
- Link to Amazon: Text, with no HTML formatting
In the “Custom Fields” tab of the dashboard, choose “Add New” to establish a field group named “Books.” After that, press the “Add Field” to create the required fields as needed.
Assigning Field Group to the Custom Post Type
In the “Location” section, you will define the criteria that determine when the field group appears. For this example, choose “Post Type” equal to “Book.” Additionally, you can configure field groups based on specific pages, posts with designated categories or tags, and the template being utilized. Once the specified criteria are met—like when a post is assigned to a particular category—the field group will automatically display.
Select Display Options
In the final panel, you can configure how your field group will be displayed. You can choose to show it in a metabox or opt not to. Additionally, you can decide whether it appears in the main column or the right sidebar of the dashboard. You also have the option to hide other default WordPress inputs when the field group is displayed.
Publish
When you are satisfied with your field group, press the “Publish” to activate it immediately. Your field group will then be displayed on any add or edit post screen that meets the defined criteria. In our example, the post add and edit screen will appear exactly as shown in the image below.
The “Add Post” screen for our custom post type “Book” will prominently display the custom fields defined with ACF, seamlessly integrated with the native WordPress fields.
Using Custom Fields
Using custom fields in your template is straightforward and efficient. While WordPress provides essential template tags within The Loop—like `the_content()` for the content and `the_title()` for the post title—Advanced Custom Fields takes it a step further with the `the_field(‘field_name’)` function.
This allows you to effortlessly display any custom field. If you need to process the field further rather than just displaying it, use the `get_field(‘field_name’)` function. Remember, custom fields are also readily available through shortcodes, enhancing your flexibility in content management.
Conclusion
Custom Post Types in WordPress empower users to achieve remarkable results, but their true potential is only realized with a powerful method for adding custom fields. The Advanced Custom Fields plugin is essential for filling the gaps in WordPress’s core functionality and should be a crucial element of your workflow.
Leave a Reply