Home / Developing for WordPress / Guide to understanding Meta Data and when to use it

Guide to understanding Meta Data and when to use it

WordPress started as a blogging platform and turned itself into a powerful CMS, running a variety of mid-size and large projects all over the world. Posts and pages are the beginning of the custom post type generation, just like categories and tags form the first pair of custom taxonomies. How about all the properties that we want to add to our items? Custom fields are on the right track for this venture.

What are Custom Fields?

In simple words custom fields are additional information that you can add in Posts and Pages, as well as to any custom post type defined to your system. A good starting point is to check the WordPress Codex, where custom fields are described in details and you can find some examples.

In our tutorial we will add Product Name and Product price to the post and we will display it bellow the post title. Also we will make price discount for our registered users.

Setting the stage

We have to enable (if not enabled already) Custom Fields from Screen Options in our Post Editor.

 

screen_options_custom_fields-1024x182When we are ready the Custom Fields Area should be visible right bellow our WYSIWYG post editor.

Adding our first Custom Field

Go to Custom Fields and click the “Enter New” button.

custom_fields_add_new

In the “Name“field we should define the name of our Custom Filed (in our case “product_name“) and in “Value” – our custom filed’s value (for instance, “Butterflies“).

Now, click on “Add Custom Filed” button and there we go – we have created our first custom filed.

custom_filed_product_name

We can repeat step two and do the same, but this time we will add product price.

Again we go to Custom Fields, click Enter New. For Name add product_price and for Value add 100.

Now we have our two Custom Fields and we can proceed to the next step.

two_custom_fields

Display our Custom Fields below Post title.

For the sake of our example we would be using a basic WordPress installation and display our content based on the Twenty Eleven theme.

Now go to Appearance => Editor and from Twenty Eleven files choose content-single.php(or single.php, it depends of your WordPress theme).

We have to display Product Name and Product Price bellow Post title, so will place our code somewhere bellow Post title.

appearance_editor_content_single-1024x406

We will use get_post_meta() WordPress and update_post_meta() functions.

<?php
/*
* We will use this place for our Custom Fileds
*/

// set defaults for our name and price should they be empty

$product_name_def = ‘Default name’;

$product_price_def = 0;

$product_name = get_post_meta($post->ID, ‘product_name’, true);
// check if the custom field has a value
if (empty ( $product_name )) {
$product_name = $product_name_def;
}

$product_price = get_post_meta($post->ID, ‘product_price’, true);
// check if the custom field has a value
if ( empty( $product_price ) ) {
$product_price = $product_price_def;
}

$vat = 1.2;
$product_price_vat = ($product_price * $vat);

$product_price_updated = update_post_meta($post->ID, ‘product_price_vat’, $product_price_vat);

if (is_user_logged_in()) {
echo ‘The price of <strong>’. $product_name .’</strong> is: <strong>$’. $product_price .’</strong>.’;
} else {
echo ‘The price of <strong>’. $product_name .’</strong> is: <strong>$’. $product_price_vat .’</strong>.’;
}
?>

the_code-1024x449

Let’s revise the sample code above.
We use the get_post_meta($post_id, $key, $single); function.

where:

$post_id – The ID of the post from which you want the data. Use $post->ID to get a post’s ID.
$key – A string containing the name of the meta value you want.
$single – this is a boolean variable. When it’s set to true, it returns the value as a string. A custom field can have multiple values, in which case you would set this variable to false, returning an array instead.

And also we use update_post_meta($post_id, $meta_key, $meta_value);

where:

$post_id – The ID of the post from which you want the data. Use $post->ID to get a post’s ID.

$meta_key – is the key of the custom field you will edit.

$meta_value – is the new value of the custom field.

Using the is_user_logged_in() function creates a scenario for our sample where logged users of ours see the flat price of our product where regular visitors are only able to see the price with VAT added.

The Result

Logged user:

 

the_result_logged_userRegular user (non-logged):

the_result_not_logged_user

And in our post editor you will notice that we have third custom filed product_price_vat added automatically from WordPress.

third_custom_field

Conclusion

Custom fields are a powerful engine helping us to define extra data to our posts or custom post types. You can extend this scenario and use the standard post types available in your setup and add different field types: price, area (for real estates), dimensions (for some property), age (for user profiles) and so on. With further actions you can apply validation rules and assign them to your custom fields, operate with 3rd party APIs such as Google Maps or Real Estate catalogs – everything within your WordPress installation.

About admin

Check Also

responsive

Options for Responsive Themes

Over the past six months it seems that there has been a large push for ...

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>