Clicky

Tutorials
wordpress-custom-fields

Creating, Managing & Using WordPress Custom Fields: The Complete Guide

The WordPress post editor makes it easy to add or edit a post. It comes with input fields to add the post title, content, category, tags, etc.

But what if you need more?

Say, you run a blog reviewing movies. And you want to add a rating to each movie post.

How do you do that?

Sure you can add the ratings to the post content. But what if you want to filter or sort reviews based on ratings. What if you want to show the top ten movies in your sidebar?

That’s where custom fields can help.

While anyone can create or manage custom fields; using them requires some theme development experience. That’s why this article is geared towards theme developers.

Whether you’re new to WordPress theme development or an experienced developer, this article will help you learn how to create, manage and use custom fields.

What are WordPress custom fields?

You can think of custom fields in WordPress as properties of an individual post. Just like the title, published date, author, etc.

In technical terms, custom fields are post metadata.

Using WordPress custom fields has the following advantages and disadvantages.

Advantages of using custom fields

  • Easy input: Custom fields make it easy for your clients and users to add extra metadata to a post.
  • Easy filtering & sorting: As a theme developer you can easily filter or sort posts using the custom fields metadata.

Disadvantages of using custom fields

  • No control: Custom fields can be created and edited by anyone. A slight spelling mistake can cause your custom fields logic to stop working.
  • No validation: Custom field input cannot be validated. So you don’t know if the data entered is valid.

The disadvantages of custom fields can be overcome using the WordPress meta box API. However, using the meta box API requires more coding.

How to enable custom fields in WordPress?

enable-custom-fields

The custom fields user interface is but hidden in plain sight in the post editor. To unhide the custom fields UI in the new block editor:

  1. Click on the block editor menu — the three vertical dots in the top right corner of the post editor. Then click on ‘Options’.
  2. Finally, check the ‘Custom Fields’ option in the ‘Advanced Panels’.
  3. If you’re using the classic editor, you can unhide the custom fields UI from the ‘Screen Options’ tab in the top right of your post editor.

How to create & manage custom fields?

custom-fields-ui

Now that you’ve enabled the custom fields UI in the post editor, let’s look at how to use them.

A custom field has two parts — ‘Name’ and ‘Value’.

The ‘Name’ part identifies the custom field. When using a custom field, you’ll refer to it by this name.

The ‘Value’ part contains the value of the custom field.

You can add more custom fields by clicking the ‘Add Custom Field’ button.

Editing a custom field value is equally easy. Just change the ‘Value’ field when editing a post. But, you need to be careful when changing the ‘Name’ field. Your theme may break if it’s still using the old ‘Name’ value.

How to use custom fields in your theme?

Using or displaying a custom field requires some theme coding knowledge.

You can edit your site’s theme from ‘Appearance > Theme Editor’.

However, when the theme updates, your changes will be overwritten. That’s why it’s recommended to add your changes to a child theme.

Displaying custom fields

Custom fields can be displayed within the WordPress loop. In your theme, look for code that looks something like this:

<?php while ( have_posts() ) : the_post(); ?>

and

<?php endwhile; ?>

You can display a custom field anywhere within these two lines of code using:

<?php echo get_post_meta( get_the_ID(), ’name’, true ); ?>

Where ‘name’ is the value of the ‘Name’ part of the custom field.

Display posts with a specific custom field value

Custom fields make it easy to query and show posts with a specific custom field value.

Here’s some quick and dirty code to get posts with a rating value of ‘5’.

$args = array(
  'meta_key' => 'rating',
  'meta_value' => '5',
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : the_post();
  // Code to display the post.
endwhile;

Ordering posts based on a custom field value.

You can also order posts based on a custom field value.

To show posts in the descending order of their rating, here’s what you’ll need to do.

$args = array(
  'order' => 'DESC',
  'orderby' => 'meta_value_num',
  'meta_key' => 'rating',
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : the_post();
  // Code to display the post.
endwhile;

Managing custom fields using plugins

You can extend any WordPress functionality using plugins. Custom fields are no exception. Here are the top 3 plugins to create, manage and use custom fields.

Advanced Custom Fields

advanced-custom-fields

Advanced Custom Fields is a popular WordPress plugin with 1 million+ active installs. The plugin is simple, intuitive and comes with 30+ types of custom fields. It comes with a field builder and function tags to display the fields.

Meta Box

metabox-custom-fields

Similar to Advanced Custom Fields. But comes with 40+ types of custom fields. And unlike ACF, it has inbuilt support for repeatable fields.

Pods Custom Content & Field Types

pods-custom-content-fields

Again very similar to the above two plugins. But the Pods plugin also allows you to create custom post types and custom taxonomies.

Final word

Now you know how to create, manage and use custom fields.

It might seem a bit confusing at first. But once you get the hang of it, it becomes quite easy. Plus you have plugins that can help you manage your custom fields better.

What uses of custom fields can you think about? Let us know in the comment below.

And, if you liked this post, don’t forget to share it with your friends.

frank-hamiltonFrank Hamilton has been working as a translator at translation service TheWordPoint. He is a professional writing expert in such topics as blogging, digital marketing, and self-education. He also loves traveling and speaks Spanish, French, German and English.

Load More Related Articles
Load More By Editorial Team
Load More In Tutorials

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also

How to Embed Video In WordPress

Having video content on your blog post is ...

Join Over 50,000+ Subscribers

Interested In
  •   
  •           
  •                      

Search

Send this to a friend