Before we talk about custom fields in WordPress…
The WordPress editor helps you create and edit entries. It contains fields to add the title, content, categories, tags, all that.
But what if you need more fields?
Let’s say, for example, you have a recipe blog, and you want to add a field with the cooking time, how do you do it?
Okay, you’ll say there are Schema plugins that allow you to add this kind of information, but what if you want to filter results by recipes based on cooking time? Or if you want to make a list of the recipes that require less time?
That’s where the custom fields come in.
While it’s easy to create and manage custom fields, using them requires some theme development experience, so if you haven’t yet entered the wonderful world of WordPress theme creation, this article is still not for you.
However, whether you are just starting in theme development or you are an experienced programmer, I hope this guide will help you create, manage, and use custom fields in WordPress.
What are WordPress custom fields?
You could say that custom fields in WordPress are like the properties of a particular entry.
Just like the title, date of publication, author, etc., – technically – the custom fields are medadata of the entry.
Like everything else, using custom fields has its advantages and disadvantages.
Advantages of using custom fields
- Easy to complete: Custom fields make it easy for your customers and users to add additional metadata to an entry.
- Easy to filter and sort: As a theme developer you can easily filter and sort entries using the custom field metadata.
Disadvantages of using custom fields
- No control: Anyone can create custom fields and anyone can modify them if they have access to the editor. The slightest typing error can cause the logic of the created custom fields to not work.
- No validation: You cannot validate the entry of a custom field, so you will not know if the data you have entered is valid or not.
These drawbacks of custom fields can be avoided by using the WordPress Meta Box API, but using this API requires a little more development effort and code, although I encourage you to get to know it and apply it.
How do I activate custom fields in WordPress?
Depending on the editor you are using, the custom fields are activated in one way or another in WordPress.
If you are still using the Classic Editor you have to display the “Screen Options” in the upper right part of the editor and check the corresponding box to have them displayed.
On the other hand, if you already use the Gutenberg block editor, it’s a bit more tedious, because there are more clicks.
To start, you have to go to the 3 dots icon on the top right of the editor, the “More tools and options” one and activate the option at the end, “Options“.
In the pop-up window you will see you have to click to activate the advanced panel of custom fields.
And then confirm in the button “Enable and reload” the personalized fields before you can even see them in the editor.
Why are there 3 steps?
Well, because currently the block editor has support for custom fields “like that”, and you should know that is a full reload of the editor, if you have not saved changes in your content you will lose them when you activate them.
Well, in any case, we already have the custom fields. You will see this…
How are custom fields created and managed?
Now that you have activated the custom field interface in the editor let’s see how they are used.
To begin with you will see that a custom field has two parts:
- Name: Identifies the custom field. When you use a custom field you refer to it by its name.
- Value: Contains the value of the custom field.
You can add more custom fields by clicking the “Add new custom field” button
So far so good, right?
Editing a value of a custom field is just as easy, you just have to change the value field. But you have to be careful when changing the name field because your theme may break if it is still using an old custom field name.
How do I use custom fields from a theme?
In order for users to see or use a custom field, someone must have programmed it first.
If this is your first time, and you are simply adding custom fields to an existing theme, to experiment, I recommend that you do so in a child theme.
How to display custom fields
To display the custom fields in the WordPress loop the first thing to do is to search in the theme for something like this:
while ( have_posts() ) : the_post();Code language: HTML, XML (xml)
endwhile;Code language: HTML, XML (xml)
Once located you can display a custom field between those two lines of code using this:
echo get_post_meta( get_the_ID(), ’name’, true );Code language: HTML, XML (xml)
I think you will have guessed that
name is the value of the name part of the custom field.
How to display entries with a particular value from a custom field
As I said at the beginning, the custom fields make it easy to find and display entries from custom field values.
Here is an example of code that would show entries with a cooking time of 5 minutes, following the idea I gave you at the beginning of this guide.
$args = array( 'meta_key' => 'cooking-time', 'meta_value' => '5', ); $the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : the_post(); // Code to show entry. endwhile;Code language: PHP (php)
How to sort entries based on the specific value of a custom field
As I told you before, you can also order based on a value in a custom field.
To display entries in descending order according to the cooking time (to follow the example) would be done like this:
$args = array( 'order' => 'DESC', 'orderby' => 'meta_value_num', 'meta_key' => 'cooking-time', ); $the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : the_post(); // Code to show entry. endwhile;Code language: PHP (php)
Creation and management of custom fields using plugins
Well, you know that if you want to extend the functionality of WordPress that you have the plugins.
Well yes, you can also create and manage custom fields in WordPress using plugins, here are the most popular one.
Advanced Custom Fields
ACF is currently the rock star of custom field creation and management in WordPress.
It is very easy to use but has really powerful features. It starts as soon as you install it with more than 30 types of fields, and it has an extensive documentation.
It is by far the most used.
Very similar to ACF, it was for a long time the standard and recommended.
In addition to custom fields, it allows you to create custom taxonomies and settings pages for your custom fields, among other features.
Another competitor in the battle for custom fields.
Meta Box has over 40 types of custom courses and is offered as the easiest way to create, duplicate and customize custom courses.
Whether you choose to create custom fields manually or using plugins, they are a great way to extend the functionality of WordPress and you should at least try them out and evaluate their possibilities.
It’s also true that custom fields are a feature whose popularity in WordPress comes and goes.
But what is unquestionable is their enormous possibilities when it comes to customizing the contents of a website.
Read this post in Spanish: Campos personalizados en WordPress: Qué son, cómo se crean y cómo se usan