Custom fields are a feature of WordPress that allow you to store additional information about your posts or pages, such as author, date, price, category, etc. Custom fields are also known as post meta or meta data, and they are stored in the wp_postmeta table in the database.

To retrieve and display custom fields on the front end of your website, you need to use the WordPress get_post_meta function. This function allows you to access the value of a specific custom field for a given post or page, and show it to your visitors.

In this tutorial, we will explain what the WordPress get_post_meta function is, what parameters it has, and how to use it to display custom fields in your theme or plugin. We will also show you some examples of how to use the get_post_meta function in different scenarios.

What Is the WordPress get_post_meta Function?

The WordPress get_post_meta function is a built-in function that lets you get the value of a custom field for a specific post or page. The function is defined as follows:

get_post_meta( $post_id, $key, $single );

The function has three parameters:

  • $post_id – This is the ID of the post or page that you want to get the custom field value from. You can use get_the_ID() function to get the current post ID, or specify a numeric value for a different post.
  • $key – This is the name of the custom field that you want to get the value from. You can leave this parameter empty to get all the custom fields for the given post ID.
  • $single – This is a boolean parameter that determines whether you want to get a single value or an array of values for the custom field. The default value is false, which means that the function will return an array of values if the custom field has multiple values. If you set this parameter to true, the function will return only the first value of the custom field.

The function returns different values depending on the parameters and the existence of the custom field:

  • If the $post_id is invalid (non-numeric, zero, or negative), the function returns false.
  • If the $post_id is valid but does not exist in the database, the function returns an empty string.
  • If the $key is empty, the function returns an associative array of all the custom fields and their values for the given post ID.
  • If the $key is specified but does not exist in the database, the function returns an empty array.
  • If the $key is specified and exists in the database, and $single is false, the function returns an array of values for that custom field.
  • If the $key is specified and exists in the database, and $single is true, the function returns a single value for that custom field.

How to Use the WordPress get_post_meta Function to Display Custom Fields

To use the WordPress get_post_meta function to display custom fields on your website, you need to add some code to your theme or plugin files. You can use any PHP file that is relevant to where you want to show the custom field, such as single.phppage.phpfunctions.php, etc.

The basic syntax of using the get_post_meta function is as follows:

<?php
// Get the value of a custom field
$value = get_post_meta( $post_id, $key, $single );

// Display the value
echo $value;
?>

You can replace $post_id$key, and $single with your desired values, depending on what custom field you want to show and how.

For example, if you want to display a custom field called “price” for the current post, and you know that it has only one value, you can use this code:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get the value of "price" custom field
$price = get_post_meta( $post_id, 'price', true );

// Display the price with a currency symbol
echo '$' . $price;
?>

This code will output something like this:

$49.99

If you want to display a custom field called “category” for a specific post with ID 123, and you know that it has multiple values, you can use this code:

<?php
// Specify a post ID
$post_id = 123;

// Get an array of values for "category" custom field
$categories = get_post_meta( $post_id, 'category', false );

// Display each category with a comma separator
echo implode( ', ', $categories );
?>

This code will output something like this:

Books, Music, Movies

WordPress get_post_meta Function Examples

Here are some more examples of how to use the WordPress get_post_meta function in different situations.

Example 1: Check if a Custom Field Exists

If you want to check if a custom field exists for a given post or page, you can use the !empty() function to test the result of the get_post_meta function. For example, if you want to check if a custom field called “rating” exists for the current post, you can use this code:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get the value of "rating" custom field
$rating = get_post_meta( $post_id, 'rating', true );

// Check if the custom field exists
if ( !empty( $rating ) ) {
  // Display the rating with stars
  echo 'Rating: ' . str_repeat( '★', $rating );
} else {
  // Display a message if the custom field does not exist
  echo 'No rating available';
}
?>

This code will output something like this:

Rating: ★★★★

or

No rating available

Example 2: Display All Custom Fields for a Post

If you want to display all the custom fields and their values for a given post or page, you can use a foreach loop to iterate over the result of the get_post_meta function with an empty $key parameter. For example, if you want to display all the custom fields for the current post, you can use this code:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get an array of all the custom fields and their values
$custom_fields = get_post_meta( $post_id );

// Loop through each custom field
foreach ( $custom_fields as $key => $value ) {
  // Display the custom field name and value
  echo $key . ': ' . $value[0] . '<br>';
}
?>

This code will output something like this:

price: 49.99<br> category: Books<br> rating: 4<br> author: John Smith<br>

Example 3: Display a Custom Field with HTML Formatting

If you want to display a custom field with some HTML formatting, such as a link, an image, or a list, you can use HTML tags along with the get_post_meta function. For example, if you want to display a custom field called “image” that contains an image URL for the current post, you can use this code:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get the value of "image" custom field
$image = get_post_meta( $post_id, 'image', true );

// Display the image with an HTML img tag
echo '<img src="' . $image . '" alt="Post Image">';
?>

This code will output something like this:

<img src=“https://example.com/wp-content/uploads/2023/11/post-image.jpg” alt=“Post Image”>

Conclusion

In this tutorial, we have shown you what the WordPress get_post_meta function is, what parameters it has, and how to use it to display custom fields on your website. We have also given you some examples of how to use the function in different scenarios.

We hope you found this tutorial helpful and easy to follow. If you have any questions or feedback, please leave a comment below. Thank you for reading!

Categorized in: