Working with Custom Meta Boxes and Fields

Custom fields are part of the WordPress for the very early version. Custom field or meta data is available for each post and you can add any number of fields you want. But, working with custom fields is not that easy due to very limited interface WordPress provides by default.

Before we proceed, you can read more on custom fields from WordPress documentation:


GD Custom Posts and Taxonomies Tools Pro (CPT Tools from now on) has a solution for these problems. Instead of generic and limited custom fields editor, plugin allows you to define more usable groups of fields as custom meta boxes. Meta box is part of the post editor panel (for any post type), and can be used for many thing. Default WP meta boxes are used for assigning categories and tags, publishing and more. One meta box is a widget like control and you can move it around the post editor panel like any other default meta box WordPress uses. CPT Tools allow you to set any number of meta boxes and assign them to one or more post types. Before you set up meta box, you need to define custom fields. Each field needs a name, code and description as well as the type of the field. This type setting is very important because it will help you define and limit the use of the field to specific use: number, date, color, radio button selection, image… To see the setup for the fields and meta box you can watch the video here:…/custom-meta-boxes-in-gd-cpt-tools-3-0/.

Custom fields in WordPress allow you to have more than one value with same name for each field for each post. This can be confusion for users, and in many years I use WP, I never had any need to use that, so plugin by default is not allowing more than one value for custom field for each post and will make it much easier to handle. Now that you know how to set the fields and meta boxes, it’s time to see how to use them and display values.


Plugin adds one shortcode with various arguments to display shortcodes. Basic documentation for this shortcode is here: Some of the arguments for the shortcode are required, and for this shortcode that is the code. This code is the same you used to define custom fields used in custom meta boxes previously. So, the simples way to implement it is like this (all shortcode examples have extra space between character [ and shortcode name to prevent WordPress for replacing the code, when you use it, there must be no space there):

[ cpt_field code=”movie_year” ]

This will display value for custom field movie_year. This simple version of the code can be added by the meta box plugin adds, where each field has small button that will insert the shortcode for the field into the post, this is shown in the video previously.

Documentation shows more arguments you can use. If you add post argument, shortcode will pull value from the post you set ID to. More complex rendering allows you to envelop value for the field into a HTML tag. So, if you want to put value into DIV with class and style set, shortcode will look like this:

[ cpt_field code=”movie_year” tag=”div” class=”movie-css-value” style=”display: block; color: red;” ]

For the image custom field (plugin stored URL as the value), if you use this shortcode you will see the actual image, because plugin will generate IMG tag. To control that, argument you need is image. It can have values ‘url’ or ‘img’, and what you set, you will get either HTML tag or URL only. Additional tag, class and style arguments can be used for image also enveloping IMG tag into another tag.


As I said before, you can use simple WP function for getting value of the custom field. This default function is described in the WP documentation: So, to get the value for the movie_year custom field, function call will look like this:

$value = get_post_meta($ID, “movie_year”, true);

$ID is the post ID. If you do this in the loop, you can replace this with $ID for the current post like this:

$value = get_post_meta(get_the_ID(), “movie_year”, true);

The value you get is raw, and for some complex custom fields is not very useful because you need to perform additional processing. So, you can use function added by CPT Tools plugin. Function documentation: This function instead of third single parameter from WP function uses array with additional arguments, same ones from the shortcode earlier. In basic form, using this function example from above will be:

$value = gdtt_get_post_meta(get_the_ID(), “movie_year”);

And if you need to envelop it in tag with CSS class and style function will be like this:

$value = gdtt_get_post_meta(get_the_ID(), “movie_year”, array(tag => “div” class => “movie-css-value” style => “display: block; color: red;”));

You only now need to echo the value to display it. Best thing for this function is that it will support special custom fields for images or color or anything else and if needed, more arguments will be added and documented as needed to allow greater flexibility.


Leave a Reply