black friday sale
Optimizing Database for Custom Fields in WordPress

Optimizing Database for Custom Fields in WordPress

Custom fields is a way for WordPress to store arbitrary extra data for content (posts and custom post types), such as author name, published date for a book. To make custom fields flexible and compatible with different kinds of data, WordPress designs the meta tables (post meta, term meta, user meta, and comment meta) in the form of key-value. According to that, each custom field is stored as one row in the database. This approach allows developers to store unlimited data regardless of its structure. But, the downside is the rapid bloat of the database. Because the number of custom fields is usually very large. This article will present solutions to optimize the storage of custom fields in the database to help boost your website performance.

Continue reading "Optimizing Database for Custom Fields in WordPress"

What is custom fields in WordPress

What is Custom Fields in WordPress?

Custom Fields is an important and useful feature in WordPress and bring us a very high customization ability. So, let's find out the basics, relevant techniques, and excellent applications of Custom Fields in this series.

If you've already reached WordPress, you also heard that it is a top platform for quickly building blogs. Furthermore, its partial actual power is Custom Fields, which took part in the WordPress' strong development and taking up leading position in CMS over previous 15 years.

About WordPress

Currently, WordPress is the most popular CMS in the world that powers around 43.1% of websites. It is the statistic on public sites only. There is also a large number of invisible sites such as deep web or private sites which no one knows for sure. We merely know that the number of such sites is much larger than the public.

It's not to say that WordPress is born with a destined destiny to lead the world. It's the result of its long-term growth history with features are able to satisfy lots of users, over 45,000 plugins, and about 6,000 themes published officially on wordpress.org.

Why can WordPress be able to meet the various different needs from news sites, portfolio sites, and hotel reservations sites to e-commerce sites of such a large number of users? What are the secrets? One of the answers is Custom Fields and the metadata architecture behind.

First of all, let's inquire basically how data is organized in WordPress and why metadata is so important.

Metadata

We could interpret Metadata as arbitrary data (meta) attached to the (primary) data providing more information and clarifying that (primary) data. The primary data does not contain this meta itself.

There are main types of object in WordPress are Post, User, Comment, Taxonomy, and Media. They hold only essential fields provided by WordPress to describe them. E.g. Title for Product or Post, user_name for User, and Name for Category. In the database, these all things are expressed in correspondent tables with columns (tables for objects and columns for fields).

Objects and Fields in correspondent tables with columns in WordPress
Objects and Fields in correspondent tables with columns in WordPress

However, what if you want to depict the object Post as a Product? So you need additional information on price, quantity, and color for instance. So that, instead of adding columns to the table ordinarily, the data of additional information will be saved into metadata, then linked to Post. Post becomes Product from now on.

It means that Post is Post only without metadata. In contrast, Post could become Product when you use metadata. Then, if you do not want it to be Product anymore, you may associate it with fields for Room as price and space, so Post becomes Room without changing the fundamental data of Post. That highly interested flexibility is the foundation for the power of WordPress' growth.

Custom Fields

Now, we come back to Custom Fields.

In WordPress, to allow users to add, delete, or edit metadata data, there must be a tool for manipulating them. That is the custom fields!

So, the principal question here is what custom fields is?

Custom fields is a feature that helps us create forms and fields to interact with metadata.

Here is a screenshot of how custom fields are implemented in WordPress.

Forms and fields of custom fields to fill data
Forms and fields of custom fields to fill data

If there is no user interface (UI) of the custom fields as above, the only way to add, remove, or edit metadata is using the code through the functions provided by WordPress. For WordPress beginners, this is quite difficult. A UI will help them manipulate the data more visually and conveniently.

In fact, the concept of metadata is rarely mentioned because it is a technical term hidden under custom fields in WordPress. And when it comes to custom fields, it's generally understood that it includes both the user interface and the metadata. In this series, we also agree with that and use it for both.

Sorts of custom fields and metadata

As mentioned above, custom fields create form fields so that user can add metadata that they need. So which objects will this data be added?

In the metadata section, we said that the main types of objects are Post, User, Comment, Taxonomy, and Media. All of these objects may have own custom fields, so we have corresponding custom fields and metadata also for Post, for User, and so on. You also can see custom fields when you edit them.

Here is a screenshot of the custom fields for posts:

Illustration for custom fields when editing posts
Illustration for custom fields when editing posts

Here is a screenshot for custom fields for categories:

Illustration for custom fields when editing categories
Illustration for custom fields when editing categories

Common types of custom fields

As you can see in the screenshot above, there are many types of data need to add to each object usually. It can be a short text, an image, or a URL.

In the process of carrying out the content, users often encounter the following field types:

  1. Text field (e.g. name, address)
  2. Image field (e.g. profile image, icon)
  3. File upload field: PDF file download, invoice download
  4. Editor field: brief description, intro text
  5. Relationship field: field links to another object types
  6. Repeater field: field can be repeated
  7. URL field: website, social media profile
  8. Choice field: select an option from a predefined list
  9. And many more

To add such custom fields to WordPress, we have the following three options:

  1. Use the default feature of WordPress
  2. Use the functions of WordPress
  3. Use plugins

For some simple fields such as text, the WordPress default functions are sufficient to use. Nevertheless, if you want to use more complex fields, you must manually code it. It's not too hard for fields like image and editor, but it's complicated for fields like repeater or relationship fields.

Applications of custom fields

You definitely can create websites in different industries with custom fields. For example:

  1. Travel: add descriptive fields for Address, Rating, Price, Tour guide.
  2. Hotel: add fields such as Price, Availability, Capacity, Facilities.
  3. E-commerce: fields about Price, Volume, Quantity.

In WordPress, we can say that wherever custom post types are, there custom fields are!

Conclusion

Metadata and custom fields have provided WordPress a powerful capability of customization to meet the needs of nearly 30% website in the world, in all industries. When you are working with WordPress, you either only build a trivial blog or cannot ignore Custom Fields.

On the other hand, custom fields are also a cause of many disputes. Some people say it makes query slower, and some say it is not suitable to scale. So that to know the truth, there is no other way than to learn more about WordPress' building and querying data from custom fields' database. Please be patient, we will learn all about them step by step in this series, in later articles.

Besides, there is a term called custom taxonomies. Someone may confuse when to use custom fields or custom taxonomies. If you have the same question, you should read our article about Custom Fields vs. Custom Taxonomies.

Easy way to add custom fields in WordPress without plugins

Easy Way to Add Custom Fields in WordPress Without Plugins

In the previous post, we have learned the basic concepts of custom fields and their applications. In this post, we will learn how to add custom fields to a WordPress site without coding or using a third-party plugin.

We have said in the latest post that WordPress has a default function that helps end-users manage custom fields in a simple way. You may add as many fields as you want with any names you like.

Let's get started to find them out!

Continue reading "Easy Way to Add Custom Fields in WordPress Without Plugins"

How to Create Custom Meta Boxes and Custom Fields in WordPress

How to Create Custom Meta Boxes & Custom Fields in WordPress?

The default custom fields functionality in WordPress is to help end-users add content and customize their websites fast and easily. However, it is limited to only text fields and thus, is not enough in most cases. Then you may want to look for a plugin that can help you create fields in any type to save any kind of data as Meta Box plugin, it’s so easy. But you know what, even when you don’t want to use any plugin from others, you can create one by yourselves to create new custom fields in your totally new types.

Continue reading "How to Create Custom Meta Boxes & Custom Fields in WordPress?"

Get Posts by Custom Fields in WordPress

Get Posts by Custom Fields in WordPress - Part 1

In the previous articles, we've known about what Custom Fields is and the application of custom fields which allows users to add arbitrary information into posts. But what do you do after that? In the recent post, you also learned how to add custom fields programmatically and display them in your theme. But you’ll see a shortcoming if there is no classification and searching. This post will guide you on how to get posts by custom fields in WordPress and we'll create an interesting application: advanced search.

Continue reading "Get Posts by Custom Fields in WordPress - Part 1"

How does WordPress Store Custom Fields

How does WordPress Store Custom Fields? #1: Data flow

Until now, we’ve known what custom fields is and how to use the functions provided by WordPress to work with custom fields. You’ve prepared everything needed to develop practical applications. But wait, before embarking on doing something new, you had better dig a bit deeper to have thorough understand custom fields' nature. It’s time to find out an answer to the question: “What really happens with custom fields when I click Save post?”. The two coming posts will give you the answers.

Continue reading "How does WordPress Store Custom Fields? #1: Data flow"

Get Posts by Custom Fields in WordPress - Part 2

Get Posts by Custom Fields in WordPress - Part 2

In part 1 of this series, we learned about how to get posts by custom fields in WordPress with the WP_Query function. However, if we need to deal with databases, it’s better to find another faster, easier, and more convenient way. That method is joining wp_posts and wp_postmeta tables together, and then querying by SQL. In part 2 of the series “How to get posts by custom fields in WordPress”, we’ll use this way to add the advanced searching feature that allows users to search by keywords in posts / pages titles and custom fields content.

Continue reading "Get Posts by Custom Fields in WordPress - Part 2"