Domains.co.za
  • Domains
    Register Domains
    Register your perfect domain today.
    It’s quick and easy.
    AI Domain Name Generator AI Domain Name Generator
    Create your perfect Domain Name today.
    Transfer Domains
    Transfer your domains to us in a few easy steps.
    Whois Search
    View the whois information on a domain name.
    Bulk Domain Registrations
    Register and check the availability of multiple domains at once.
    Bulk Domain Transfer
    Move multiple domains to us quickly and easily.
    Domain Protection
    Protect your personal information.
  • Hosting
    cPanel Hosting Web Hosting
    cPanel Web Hosting From R109/mo
    WordPress Hosting
    Experience the best in WordPress Hosting
    Email Only Hosting
    Easy setup, automated updates & backups
    Site Builder Site Builder
    Create your stunning website, it's fast and easy
  • VPS
    Linux VPS Hosting
    UltraFast  VPS Hosting From R209/mo
    Windows VPS Hosting
    UltraFast  VPS Hosting From R1139/mo
    Managed cPanel Hosting Managed cPanel Hosting
    Managed cPanel Virtual Machines From R2999/mo
    Self-Hosted n8n VPS Self-Hosted n8n VPS
    Unlimited Workflow Automation From R319/mo
  • Security
    Domain Validated SSL
    Domain Validated SSL Certificates
    Organization Validated SSL
    Organization Validated SSL Certificates
  • Resellers
    Domain Reseller
    Resell Domains on our platform From R89 per domain
    Web Hosting Reseller
    Reseller Web Hosting on SSD Hosting Servers From R419/mo
  • Help
    Open a Ticket
    Log a support or billing enquiry with one of our departments.
    011 640 9700
    Need Help? Give us a call
    About Us
    Information about Domains.co.za
    Knowledgebase
    Check out our extensive Knowledge Base for help
    Network Status
    Just to check everything is hunky dory
    Accreditations
    View the organisations and registries we are accredited with
    Blog
    View insights into the latest scoops
    Articles & Tutorials Articles & Tutorials
    Practical guides and insights to help you succeed
  • Checkout
011 640 9700
Contact Support
Sign In

How To Use The WordPress get_post_meta Function

< All Topics
  1. Main
  2. WordPress
  3. How To Use The WordPress get_post_meta Function
Summarize with:
ChatGPT
Claude
Perplexity

The WordPress get_post_meta function is a useful tool for developers who need to retrieve custom field data and display it dynamically on a WordPress website. Post metadata can be used to store additional information associated with a post, page or custom post type, allowing developers to extend WordPress beyond the standard title, content and featured image fields.

For example, a website could use custom fields to store a reading time, product price, event date, author note, service label or image URL. The get_post_meta() function can then retrieve that information so it can be displayed in a WordPress template or used by other PHP code.

This guide explains how the WordPress get_post_meta function works, how its parameters are used, and how to safely retrieve and display custom field data. The examples are intended for WordPress developers, agencies, technically confident website owners and administrators with basic PHP knowledge.

WordPress stores post metadata separately from the main post content. The get_post_meta() function retrieves metadata associated with a specific post ID. WordPress describes the function as retrieving a post meta field for a given post ID, with the $single parameter determining whether one value or an array of values is returned.

The function can be used with standard posts and pages as well as custom post types. This makes it useful when building custom WordPress websites where additional information needs to be displayed dynamically.

TABLE OF CONTENTS

  • What Is the get_post_meta Function in WordPress?
  • What Are Custom Fields & Post Metadata?
  • When Would You Use get_post_meta?
  • Before You Begin
  • Important Safety Note Before Editing WordPress Files
  • get_post_meta Syntax Explained
  • Basic Example: Display One Custom Field
  • Why Escaping Output Matters
  • How to Add get_post_meta to a WordPress Template
    • Step 1: Use a Child Theme
    • Step 2: Find the Correct Template File
    • Step 3: Add the get_post_meta Code Where the Value Should Display
    • Step 4: Save and Test on the Front End
  • How to Display get_post_meta with a Code Snippet Plugin
    • Step 1: Choose a Reputable Code Snippet Plugin
    • Step 2: Add a PHP Snippet
    • Step 3: Choose Where the Snippet Runs
    • Step 4: Save, Activate and Test
  • How to Check Whether a Custom Field Exists
  • How to Retrieve Multiple Values
  • How to Display an Image Stored in Post Meta
  • How to Use get_post_meta with Custom Post Types
  • Common get_post_meta Mistakes
  • Troubleshooting get_post_meta Not Showing Data
  • Additional Information
    • Login to Domains.co.za Account

What Is the get_post_meta Function in WordPress?

get_post_meta() is a WordPress function used to retrieve metadata, also commonly called custom field data, from a specific post, page or custom post type.

Its basic syntax is:

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

The function returns the requested metadata based on the post ID and custom field key.

What Are Custom Fields & Post Metadata?

Custom fields allow additional information to be associated with a WordPress post. This information can include:

  • Reading time.
  • Product or service details.
  • Author notes.
  • Ratings.
  • Prices.
  • Locations.
  • Event dates.
  • Custom labels or badges.
  • Additional image URLs.

Post metadata should not be confused with user meta, term meta or site options, which are stored and retrieved using different WordPress functions. WordPress provides separate functions for different types of metadata.

When Would You Use get_post_meta?

You could use get_post_meta() when you need to:

  • Display a custom reading time.
  • Show an author biography or additional author information.
  • Add product or service details.
  • Display a custom image.
  • Show an event date.
  • Add custom labels or badges.
  • Retrieve information associated with a custom post type.
How To Use The WordPress get_post_meta Function

Before You Begin

Before working with get_post_meta(), ensure you have:

  • WordPress administrator access.
  • Access to a child theme, custom plugin or code snippet tool.
  • A recent backup of the website.
  • A staging website where possible.
  • The exact custom field key.
  • Basic PHP knowledge.
  • An understanding of where the information should appear on the website.

Important Safety Note Before Editing WordPress Files

Avoid editing the files of a parent theme directly. Theme updates can overwrite those changes.

Instead, use a child theme, site-specific plugin or reputable code snippet tool. A staging website is also recommended when testing PHP changes, particularly on a production website.

If a PHP change causes a critical error, check the WordPress error logs or enable WordPress debugging in an appropriate development or staging environment rather than exposing debugging information publicly on a live website.

get_post_meta Syntax Explained

The basic syntax is:

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

It accepts three main parameters:

$post_id

$post_id is the ID of the post, page or custom post type item containing the metadata.

When working with the current post inside the WordPress Loop, get_the_ID() can be used to obtain the current post ID.

$key

$key is the custom field name that you want to retrieve. Examples include reading_time, price and event_date.

The key must match the key used when the metadata was stored.

$single

$single determines whether WordPress returns one value or an array.

Setting $single to true returns a single value. Setting it to false returns an array of values.

Basic Example: Display One Custom Field

A simple example for displaying a reading time is:

$reading_time = get_post_meta( get_the_ID(), 'reading_time', true );
if ( ! empty( $reading_time ) ) {
    echo '
}

get_the_ID() retrieves the ID of the current post. The reading_time value identifies the custom field, while true requests a single value.

The if statement checks whether the field contains a value before displaying it. esc_html() then safely escapes the value for HTML output. WordPress recommends escaping data when it is output, using the escaping function appropriate for the context.

Why Escaping Output Matters

Metadata can originate from user input, plugins or database values, so it should not simply be output directly.

Use the appropriate WordPress escaping function for the output context:

  • esc_html() is suitable for text displayed within HTML.
  • esc_url() should be used for URLs.
  • esc_attr() should be used for HTML attributes such as alt, title and value.

How to Add get_post_meta to a WordPress Template

Step 1: Use a Child Theme

Use a child theme when customising theme templates so your changes are less likely to be overwritten by parent theme updates.

Step 2: Find the Correct Template File

Locate the template responsible for displaying the content. Standard posts often use single.php, while pages may use page.php. Custom post types can use templates such as single-service.php or single-product.php.

Step 3: Add the get_post_meta Code Where the Value Should Display

Add the code inside the WordPress Loop or another context where the correct post ID is available.

Step 4: Save and Test on the Front End

Save the changes and test the website. Check both posts containing the custom field and posts where the field has not been populated.

How to Display get_post_meta with a Code Snippet Plugin

How To Use The WordPress get_post_meta Function

A code snippet plugin can provide an alternative to manually editing theme files. WPCode is one example, but you should check the plugin’s current compatibility, reviews and maintenance status before installing any third-party plugin.

Step 1: Choose a Reputable Code Snippet Plugin

Install a reputable and actively maintained solution from a trusted source.

Step 2: Add a PHP Snippet

Create a PHP snippet containing your get_post_meta() code and use the correct custom field key.

Step 3: Choose Where the Snippet Runs

Limit the snippet to the required location or post type where possible. Avoid running code globally unless it is designed to do so.

Step 4: Save, Activate and Test

Test the snippet on staging first where possible. After activation, check the relevant pages and confirm that the expected information is displayed.

How to Check Whether a Custom Field Exists

You can check whether a custom field contains a value before displaying it:

$value = get_post_meta( get_the_ID(), 'custom_field_key', true );
if ( ! empty( $value ) ) {
    echo esc_html( $value );
}

This prevents empty labels or blank sections from appearing when the custom field has not been populated.

When a valid post ID and $single is true are used, WordPress returns an empty string when the requested metadata does not exist.

How to Retrieve Multiple Values

If the same custom field key contains multiple values, set $single to false:

$values = get_post_meta( get_the_ID(), 'custom_field_key', false );
foreach ( $values as $value ) {
    echo '
}

This returns an array, which should be handled appropriately before output.

How to Display an Image Stored in Post Meta

If a custom field stores an image URL, retrieve and escape it before placing it in an HTML attribute:

$image_url = get_post_meta( get_the_ID(), 'custom_image', true );
if ( ! empty( $image_url ) ) {
    echo '' . esc_attr( 'Custom image' ) . '';
}

esc_url() is designed for URLs, including URLs used in src and href attributes, while esc_attr() should be used for other HTML attributes.

How to Use get_post_meta with Custom Post Types

get_post_meta() works with custom post types because the function retrieves metadata using the relevant post ID. This means the same basic approach can be used for posts, pages and custom post types.

For example, a service custom post type could have an estimated_duration field that is retrieved and displayed using the same function.

Common get_post_meta Mistakes

Common mistakes include:

  • Using the incorrect custom field key.
  • Forgetting to retrieve the correct post ID with get_the_ID().
  • Using $single = true when multiple values are expected.
  • Displaying metadata without escaping it.
  • Editing parent theme files directly.
  • Running snippets globally without appropriate conditions.
  • Expecting a value to display when the custom field does not exist.
  • Confusing post meta with user meta, term meta or site options.
  • Assuming every custom field plugin stores data in the same format.

Troubleshooting get_post_meta Not Showing Data

If the value does not appear, check the following:

  • Confirm that the custom field key is correct, including its capitalisation.
  • Confirm that the correct post ID is being used.
  • Ensure the post contains the custom field.
  • Check whether the value is stored as an array rather than a simple string.
  • Check whether a plugin such as Advanced Custom Fields (ACF) manages the field and whether it uses a particular data structure.
  • Clear applicable WordPress and hosting caches.
  • Confirm that you edited the correct template file.
  • Check the WordPress error logs for PHP errors.
  • Use WordPress Debug Mode in a suitable development or staging environment when troubleshooting PHP problems.

If a plugin or custom field solution stores arrays or objects, additional handling may be required rather than treating the returned value as a simple string.

Additional Information

  • get_post_meta() retrieves metadata associated with a WordPress post ID and can be used with posts, pages and custom post types.
  • The $single parameter determines whether WordPress returns a single value or an array of values.
  • get_the_ID() is useful when retrieving metadata belonging to the current post within the WordPress Loop.
  • Custom field keys must match the metadata key stored for the relevant post.
  • Metadata should be escaped at the point where it is output, using a function appropriate to the output context.
  • esc_html() should be used when displaying metadata as HTML text, while esc_url() should be used for URLs and esc_attr() for HTML attributes.
  • You should avoid editing parent theme files directly because theme updates can overwrite your customisations.
  • A child theme, custom plugin or reputable code snippet solution is generally a safer approach for adding custom PHP functionality.
  • A recent backup and staging environment are recommended before testing PHP changes on a production website.
  • Custom field plugins may store complex data structures, so you should not assume every metadata value is a simple text string.
  • Post metadata is different from user metadata, term metadata and site options, which have their own WordPress APIs.
  • If a PHP change causes an error, review the WordPress error logs and test the code in a staging environment before applying further changes to the live website.

Login to Domains.co.za Account

1. Go to the Domains.co.za website Account Login page.

How To Add Domain Protection To Domain - Login in to Domains.co.za Account

2. Enter your Email and Password and click the Sign In button.

3. You will see the Domains.co.za Dashboard, displaying the Manage Account menu on the left and your Account Information, Account Overview and Open Support Tickets on the right.

How To Login To Domains.co.za Account - Domains.co.za Dashboard
Views1
Previous How To Use Redis With WordPress Hosting
Next How To Use The wp_get_attachment_image Function In WordPress
[post-views]

Categories

  • Home
  • 23 Billing
  • 69 Domains
  • 126 Email
  • 16 ESET
  • 41 General
  • 197 Hosting
  • 3 Integrations
  • 3 Link In Bio
  • 3 n8n VPS Hosting
  • 6 Pre-Sales
  • 21 Registrars
  • 21 Resellers
  • 59 Site Builder
  • 65 SSLs
  • 4 VPS Hosting
  • 84 WordPress
Domains.co.za - Web Hosting
Domains.co.za - Site Builder Add-on
Domains.co.za - WordPress Hosting
Domains.co.za - AI Domain Name Generator
Domains.co.za

We are a domain name and website hosting provider committed to offering affordable web hosting solutions that enable businesses to establish a strong online presence.

Secure Payment Gateways

Peach Logo DPO Logo PayFast Logo Visa Logo Mastercard Logo Netcash Logo Ozow Logo

Read more about our secure payments.

Hosting

  • WordPress Hosting
  • Web Hosting
  • Managed cPanel Hosting
  • Email Hosting
  • Linux VPS Hosting
  • Windows VPS Hosting
  • Site Builder
  • PHP Extended Support
  • Self-Hosted n8n VPS

Domains

  • Register Domains
  • Transfer Domains
  • WHOIS Search
  • Bulk Domain Registrations
  • Bulk Domain Transfers
  • Domain Protection

Security

  • Domain Validated SSLs
  • Organization Validated SSLs

Resellers

  • Reseller Web Hosting
  • Domain Reseller
  • Registrar Services
  • Integration
  • Domain Reseller Login

Legal

  • Terms and Conditions
  • UDRP
  • Our SLA
  • Abuse Policy
  • Disclosure Requests

Our Company

  • About Us
  • Contact Us
  • Accreditations
  • Careers

Help

  • System Status
  • Affiliate Program
  • Knowledgebase
  • Blog
  • Report Abuse
Facebook Instagram YouTube Twitter LinkedIn Google Business
Copyright © 2026 Domains.co.za. All Rights Reserved.