Subscribe
WordPress trainings, support, consulting and help

Guide to understanding Meta Data and when to use it

Guide to understanding Meta Data and when to use it

What are Custom Fields?
In simple words custom fields are additional information that you can add in Posts and Pages.

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.

1) Our First Step

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

When we are ready we have to have Custom Fields Area bellow WYSIWYG post editor.

2) Adding our first Custom Field

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

In “Name” we placed name of our Custom Filed (in our case “product_name“) and in “Value” custom filed’s value (in our case “120“).

Now click to “Add Custom Filed” button and we have created our first custom filed.

We can repeat step two and do the same, but in 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 go to the next step.

3) Display our Custom Fields below Post title.

For the sake of example we have basic WordPress installation and we use 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.

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

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

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

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

$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>.’;
}
?>

Here is a little explanation of the code above.
We use 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.

4) The Result

Logged user:

Not Logged user:

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

5) In Conclusion

Hopefully at this point you have a clear understanding of how to implement custom fields.

Leave a Reply